c++ - Does std::move on std::string garantee that .c_str() returns same result? -
i want provide zero-copy, move based api. want move string thread thread b. ideologically seems move shall able pass\move data instance new instance b minimal none copy operations (mainly addresses). data data pointers copied no new instance (constructed via move). std::move on std::string garantee .c_str() returns same result on instance before move , instance created via move constructor?
no,
but if needed, option put string in std::unique_ptr
. typically not rely on c_str() value more local scope.
example, on request:
#include <iostream> #include <string> #include <memory> int main() { std::string ss("hello"); auto u_str = std::make_unique<std::string>(ss); std::cout << u_str->c_str() <<std::endl; std::cout << *u_str <<std::endl; return 0; }
if don't have make_unique (new in c++14).
auto u_str = std::unique_ptr<std::string>(new std::string(ss));
or copy whole implementation proposal s.t.l.:
Comments
Post a Comment