Understanding String Implementation
The string class is an alias for the general template basic_string with char as its character type.
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?
Reveal 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
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.
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.
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.
Defining a Japanese String Type
Defining a string over Jchar
Given a character type Jchar that already exists, define a string type named Jstring whose elements are Jchar values.
- 1Identify the template
The general template to reuse is
basic_string, the same template that the string class is built on. - 2Identify the argument
The character type is Jchar. It goes inside the angle brackets, because that is where the element type is decided.
- 3Choose the new name
The new name is Jstring, placed on the left of the equals sign so that it refers to the result.
- 4Write 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.
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 declaration | string (ordinary case) | Jstring (custom case) |
|---|---|---|
| Template used | basic_string | basic_string |
| Character type argument | char | Jchar |
| Name the user refers to | string | Jstring |
| Element type of the sequence | char values | Jchar 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.
Common mistake Treating string and basic_string as unrelated types
Why it failsThe source says string is an alias for
basic_stringwith char as its character type, so they describe the same type.Do this insteadRead 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 likeSeeing Jstring and assuming it holds ordinary char values because it is a string
Why it failsThe name does not decide the element type. The character type argument inside the angle brackets does.
Do this insteadCheck the angle-bracket argument in the alias declaration first.
Common mistake Assuming any operation works on every string type
Why it failsThe source confirms the usual string operations for Jstring, but it does not extend that claim to every possible operation.
Do this insteadVerify the specific operation you need for the character type you are using.
Trace the Character Type
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_stringwith 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 isbasic_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_stringwith 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_stringis 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.