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 a basic_string
object is designated by charT
.
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 any basic_string
object s
, the identity &*(s.begin() + n) == &*s.begin() + n
shall hold for all values of n
such that 0 <= n < s.size()
.
See also these related questions: