I always thought an std::string
was implemented as an STL wrapper for a C char array string. But looking closely at the design, I've noticed that it doesn't give any hint or sign of being a wrapped up c-string. For all I know an std::string
could be doing anything internally!
There is the c_str()
method of course which I thought returned the internal char array, but how do I know if the method doesn't create a new c char array from whatever data it stores inside and return it?
Seriously, how has std::string
been implemented? Is it (as it appears to be) just a wrapper for a C char array, or is it something else? Or a mixture of the two? Or even can become both conditionally?
For all I know an std::string could be doing anything internally!
For all you know. The standard, of course, describes and demands certain semantics that rule anything out. It says the following on the basic_string
template:
§21.4 [basic.string] p1
The class template
basic_string
describes objects that can store a sequence consisting of a varying number of arbitrary char-like objects with the first element of the sequence at position zero. Such a sequence is also called a “string” if the type of the char-like objects that it holds is clear from context. In the rest of this Clause, the type of the char-like objects held in abasic_string
object is designated bycharT
.
And a "char-like object" is defined by the following text:
§21.1 [strings.general] p1
This Clause describes components for manipulating sequences of any non-array POD (3.9) type. In this Clause such types are called char-like types , and objects of char-like types are called char-like objects or simply characters.
This effectively means that you can stuff anything you want into basic_string
, as long as it's not an array and it is a POD (see this and this for infos on what PODs are). These char-like objects are then manipulated with the help of character traits, which define the specific behaviour of and relationship between them.
[...] but how do I know if the method doesn't create a new c char array from whatever data it stores inside and return it?
In C++03 exactly this was possible to do for the implementation, a known defect that has since been corrected in C++11:
§2.4.1 [string.require] p5
The char-like objects in a
basic_string
object shall be stored contiguously. That is, for anybasic_string
objects
, the identity&*(s.begin() + n) == &*s.begin() + n
shall hold for all values ofn
such that0 <= n < s.size()
.
See also these related questions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With