#include <iostream>
#include <string>
#include <string_view>
using namespace std;
int main() {
string a = "abcdef";
cout << (const int*)a.c_str() << endl;//0x64fe20
string_view av{ a };
cout << av << endl;//abcdef
string b = std::move(a);
cout << (const int*)b.c_str() << endl;//0x64fdf0
cout << av << endl;// bcdef
}
Possible output:
0x64fe20
abcdef
0x64fdf0
bcdef
All the point of b = std::move(a)
is to avoid copy.
We can see the string_view is bcdef
and since the entire string is on b, so the string is must be copied (at least part of it), right?
And we can see the inner string buffer a.c_str() and b.c_str() are different. Ok, you can see C++ doesn't guarantee the buffer is same, but how to implement it without copying?
I tested on both VS2022 and GCC(I forget the version) with C++20 standard.