char hello[] = "hello world";
std::string str;
str.resize(sizeof(hello)-1);
memcpy(&str[0], hello, sizeof(hello)-1);
This code is undefined behaviour in C++98. Is it legal in C++11?
char hello[] = "hello world";
std::string str;
str.resize(sizeof(hello)-1);
memcpy(&str[0], hello, sizeof(hello)-1);
This code is undefined behaviour in C++98. Is it legal in C++11?
Yes, the code is legal in C++11 because the storage for
std::string
is guaranteed to be contiguous and your code avoids overwriting the terminating NULL character (or value initializedCharT
).From N3337, §21.4.5 [string.access]
Your example satisfies the requirements stated above, so the behavior is well defined.