The string isn't copied. The internal data is directly modified.
It basically gets the internal data pointer of the actual string memory, and modifies it. Imagine doing this:
char *data = &str[0]; for(size_t i = 0; i < str.size(); ++i) { data[i] = '!'; }
The code sets every character of the string to an exclamation mark. But if the string was copied, then after the first write, the data pointer would become invalid.
Or to use another example: std::cout << str[5] << std::endl;
That prints the 6th character of the string. Why would that copy the string? C++ can't tell the difference between char c = str[5] and str[5] = c (except as far as const vs non-const function calls go).
Also, str[n] is guaranteed to never throw exceptions, as long as n < str.size(). It can't make that guarantee if it had to allocate memory internally for a copy - because the allocation could fail and throw.
(As @juanchopanza mentioned, older C++ standards permitted CoW strings, but the latest C++ standard forbids this)
shas its copy of the string from the moment it is constructed. C++03 allows copy on write, so it depends on the implementation.chars of"Hello"are always copied at some point in this code, in any version of C++.