为什么通过std::move(str1)调用后,str2的指针地址还没变呢? 难道不是已经delete str2了吗?
另外请教下:文档说:Although note that -in the standard library- moving implies that the moved-from object is left in a valid but unspecified state. 那这里的"unspecified state"是什么含义呢? state有哪些呢?
std::string str1("hello");
std::string str2("");
printf("pointer1:%p,%p\n", &str1,&str2);
str2 = std::move(str1);
cout << str1;
printf("pointer2:%p,%p\n", &str1,&str2);
输出:
pointer1:0x7fff0c5532b0,0x7fff0c5532a8
pointer2:0x7fff0c5532b0,0x7fff0c5532a8
你的代码有问题。修改如下:
你的代码的问题:
std::string
针对短字符串做了优化,这种情况下你用move和不用move是没差别的。str1.data()
和str2.data()
,而不是查看&str1
和&str2
.你会发现字符串足够长的话,指向
str2
的字符串资源就是原来str1
的字符串,也就是说没有发生字符串拷贝。