This shows you the differences between two versions of the page.
Last revision | |||
— | blog:2017:11:27:2017-11-27_-_strings_and_sso [2017/11/27 21:50] – created basz | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 2017-11-27 - strings and SSO ====== | ||
+ | since many years [[wp> | ||
+ | |||
+ | < | ||
+ | std::string value_; | ||
+ | // ... | ||
+ | std::string one{" | ||
+ | char const* ptr = one.data(); // 1 | ||
+ | std::string two{ std:: | ||
+ | // ... ' | ||
+ | // ... ' | ||
+ | cout << ptr; // valid or not? | ||
+ | </ | ||
+ | |||
+ | it used to work on one of the compilers, but not on the other. | ||
+ | |||
+ | it turned out that one of the implementations used SSO, while other did not. when there was no SSO, line (1) was ok, since '' | ||
+ | |||
+ | however when SSO kicks in, short sting may be kept on stack! no memory allocation is performed, but also '' | ||
+ | |||
+ | so is the code correct or not? NO -- it is NOT correct! why? because SSO is valid, standard-compliant implementation feature of '' | ||
+ | |||
+ | lesson learned is that one should never assume neither any particular state of moved-from object, nor a state of pointer/ | ||
+ | |||
+ | by default however, there are no guarantees for other objects. technically speaking moved-from object must only be able to safely destroy itself (i.e. call d-tor). nothing more. |