Concepts / Understanding String Implementation

Understanding String Implementation

The string class is an alias for the general template basic_string with char as its character type.

  • Programming

When Text Is Not Plain char

Most of the time a string is a sequence of ordinary char values. But text does not always come from one character set. A program may need to hold Japanese text, or text drawn from Unicode, and those characters do not all fit the default char element type. The source material faces this directly: it wants one string concept that can cover several character sets, rather than a separate class for each one.

Keep this goal in mind as you read. The rest of this article shows how one general template is reused to produce a string over a different character type, and how to find the exact place in a declaration where that character type is chosen.

What do you think happens?

In the line using Jstring = basic_string<Jchar>;, which part decides what the element type of the string will be?

  • The word using at the start of the line

  • The name Jstring on the left of the equals sign

  • The Jchar argument inside the angle brackets

  • The semicolon at the end of the line

Reveal answer
Answer:

The Jchar argument inside the angle brackets

The source says the character type argument determines the element type. The angle brackets hold that argument, so the character type is fixed there.

How string Relates to basic_string

Definition

The string class is an alias for the general template basic_string with char as its character type.

Read this definition as a relationship between three things. basic_string is the general template, a pattern that can be filled in. char is the character type that fills it in for the ordinary case. string is the familiar name that refers to the result. So string is not a separate, independent implementation. It is one specific use of basic_string, with char plugged in.

stringfamiliar class namebasic_stringgeneral templatechardefault character typeJcharcustom character typeJstringalias for a Japanese stringis an alias for basic_string with charfills the template argumentcan fill the template argument insteadalias for basic_string with Jchar
What contains what: string is an alias for one specific instantiation of basic_string, with char supplied as the character type.

The diagram shows the key point. A single general template can produce several distinct string types, each tied to its own character type. The string name is simply the most common of these results.

Reading the Alias Declaration

The source gives the pattern for defining a string over a custom character type. Assume a Japanese character type named Jchar already exists. The declaration that creates a Japanese string type is written as follows.

cpp

Read the line in three parts. The name being created is Jstring, which sits to the left of the equals sign. The template is basic_string, the general pattern being used. The argument is Jchar, which sits between the angle brackets and fills in the template. Read in this order, the question to ask about any such line is what goes inside the angle brackets.

usingintroduces an aliasJstringthe new name being created=links the name to the typebasic_stringthe template<argument list opensJcharthe argument that fills thetemplate>argument list closes;ends the declarationtakes Jchar as its argumentis defined as
Which part is the template, which part is the argument that fills it in, and which part is the new name being created?

Defining a Japanese String Type

Worked example

Defining a string over Jchar

Given a character type Jchar that already exists, define a string type named Jstring whose elements are Jchar values.

  1. 1
    Identify the template

    The general template to reuse is basic_string, the same template that the string class is built on.

  2. 2
    Identify the argument

    The character type is Jchar. It goes inside the angle brackets, because that is where the element type is decided.

  3. 3
    Choose the new name

    The new name is Jstring, placed on the left of the equals sign so that it refers to the result.

  4. 4
    Write the declaration

    Combining these parts gives the alias using Jstring = basic_string<Jchar>;. The declaration does not write a new string implementation. It names one use of the general template.

Result

Jstring is a string whose character type is Jchar. It is the Japanese string type the source describes.

Notice what changed and what did not. The only thing that differs from the ordinary string case is the character type. Everything else about the pattern is the same, which is why the same template supports both.

Part of the declarationstring (ordinary case)Jstring (custom case)
Template usedbasic_stringbasic_string
Character type argumentcharJchar
Name the user refers tostringJstring
Element type of the sequencechar valuesJchar values

The same template produces both types. Only the character type argument changes.

Which Operations Still Apply

Once Jstring is defined, the source says you can do all the usual string operations on it, because it is a string of Japanese characters. The phrase usual string operations refers to the operations you already know from string, now applied to a string whose element type is Jchar.

The source makes the same point for Unicode: a Unicode string can be handled in the same way. The practical reading is that the character type changes what the elements are, while the set of operations the source names stays the same for the new type.

Mistakes When a Result Is Unexpected

When a string result is not what you expected, the first question is where the character type was decided. For an alias like Jstring, that decision lives in the declaration that introduces the name. The source recommends checking there first, because the character type argument in that line determines the element type.

Alias declarationcheck the character typeargument firstCreation orassignment codeconfirm it uses theexpected element typeOperation producingthe resultthe unexpected outputif the alias line is correctthen trace the operation
Starting from an unexpected result, the search moves from the alias declaration to the code that creates or assigns the value.
  • Common mistake

    Treating string and basic_string as unrelated types

    Why it fails

    The source says string is an alias for basic_string with char as its character type, so they describe the same type.

    Do this instead

    Read string as the basic_string template with char filled in.

  • Common mistake

    Looking only at the variable name when the type is unexpected

    What it looks like

    Seeing Jstring and assuming it holds ordinary char values because it is a string

    Why it fails

    The name does not decide the element type. The character type argument inside the angle brackets does.

    Do this instead

    Check the angle-bracket argument in the alias declaration first.

  • Common mistake

    Assuming any operation works on every string type

    Why it fails

    The source confirms the usual string operations for Jstring, but it does not extend that claim to every possible operation.

    Do this instead

    Verify the specific operation you need for the character type you are using.

Trace the Character Type

MEDIUM

Suppose a colleague writes using Jtext = basic_string<Jchar>; and later reports that a value stored in Jtext behaves unlike a char string. Without changing any code, list the steps you would take to find where the character type of Jtext was decided, and name the exact token that controls it.

Hints
  • Start with the alias declaration that introduces the name Jtext.
  • Look between the angle brackets of basic_string.
  • Only if that line is correct should you move to the code that creates or assigns the value.

Key Takeaways

  • string is an alias for the general template basic_string with char as its character type.
  • Any string over another character type is one use of basic_string, with that character type supplied as the template argument.
  • In a declaration such as using Jstring = basic_string<Jchar>;, the template is basic_string, the argument is Jchar, and the new name is Jstring.
  • When a result is unexpected, check the alias declaration first, since its character type argument determines the element type.

At a glance

  • string is an alias for the general template basic_string with char as its character type.
  • A string over another character type, such as Jchar, is defined by supplying that type as the template argument.
  • In using Jstring = basic_string<Jchar>;, basic_string is the template, Jchar is the argument, and Jstring is the new name.
  • The source says the usual string operations apply to a Jstring, and that a Unicode string can be handled in the same way.
  • When a result is unexpected, check the alias declaration first to see where the character type was decided.

Related concepts

More concepts starting with U