c++ - std::move 如何使原始变量的值无效?
问题描述
在cpp 参考的以下示例中:
#include <iostream>
#include <utility>
#include <vector>
#include <string>
int main()
{
std::string str = "Hello";
std::vector<std::string> v;
// uses the push_back(const T&) overload, which means
// we'll incur the cost of copying str
v.push_back(str);
std::cout << "After copy, str is \"" << str << "\"\n";
// uses the rvalue reference push_back(T&&) overload,
// which means no strings will be copied; instead, the
// Contents of str will be moved into the vector. This is
// less expensive, but also means str might now be empty.
v.push_back(std::move(str));
std::cout << "After move, str is \"" << str << "\"\n";
std::cout << "The contents of the vector are \"" << v[0]
<< "\", \"" << v[1] << "\"\n";
}
使用std::move
可能会导致原始值丢失。对我来说,它看起来像
v.push_back(std::move(str))
导致v[1]
创建一个新成员。然后,
&v[1] = &str
但为什么它会损害 中的价值str
呢?它没有任何意义。
有许多复杂的教程std::move
比我自己的问题更难理解。
有人可以写吗
v.push_back(std::move(str))
在其等效使用c++03
?
我寻找一种易于理解且不包含诸如x-value
、static_cast
、remove_reference
等先决条件的解释,因为它们本身需要先理解std::move
。请避免这种循环依赖。
这些链接也不能回答我的问题:7510182、3413470
因为我有兴趣知道str
伤害是如何产生的,而不是会发生什么v[1]
。
伪代码也很受欢迎,只要它像c++03
.
更新:为避免复杂化,让我们考虑一个更简单的示例,int
如下所示
int x = 10;
int y = std::move(x);
std::cout << x;
解决方案
根据实现,这std::move
可能是内部存储器地址的简单交换。
如果您在http://cpp.sh/9f6ru上运行以下代码
#include <iostream>
#include <string>
int main()
{
std::string str1 = "test";
std::string str2 = "test2";
std::cout << "str1.data() before move: "<< static_cast<const void*>(str1.data()) << std::endl;
std::cout << "str2.data() before move: "<< static_cast<const void*>(str2.data()) << std::endl;
str2 = std::move(str1);
std::cout << "=================================" << std::endl;
std::cout << "str1.data() after move: " << static_cast<const void*>(str1.data()) << std::endl;
std::cout << "str2.data() after move: " << static_cast<const void*>(str2.data()) << std::endl;
}
您将获得以下输出:
str1.data() before move: 0x363d0d8
str2.data() before move: 0x363d108
=================================
str1.data() after move: 0x363d108
str2.data() after move: 0x363d0d8
但结果可能会因编译器和 std 库的实现而异。
但实现细节可能更复杂http://cpp.sh/6dx7j。如果您查看您的示例,那么您将看到为字符串创建副本并不一定需要为其内容分配新内存。这是因为几乎所有的操作std::string
都是只读的或者需要分配内存。所以实现可以决定只做浅拷贝:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string str = "Hello";
std::vector<std::string> v;
std::cout << "str.data() before move: "<< static_cast<const void*>(str.data()) << std::endl;
v.push_back(str);
std::cout << "============================" << std::endl;
std::cout << "str.data() after push_back: "<< static_cast<const void*>(str.data()) << std::endl;
std::cout << "v[0].data() after push_back: "<< static_cast<const void*>(v[0].data()) << std::endl;
v.push_back(std::move(str));
std::cout << "============================" << std::endl;
std::cout << "str.data() after move: "<< static_cast<const void*>(str.data()) << std::endl;
std::cout << "v[0].data() after move: "<< static_cast<const void*>(v[0].data()) << std::endl;
std::cout << "v[1].data() after move: "<< static_cast<const void*>(v[1].data()) << std::endl;
std::cout << "After move, str is \"" << str << "\"\n";
str = std::move(v[1]);
std::cout << "============================" << std::endl;
std::cout << "str.data() after move: "<< static_cast<const void*>(str.data()) << std::endl;
std::cout << "v[0].data() after move: "<< static_cast<const void*>(v[0].data()) << std::endl;
std::cout << "v[1].data() after move: "<< static_cast<const void*>(v[1].data()) << std::endl;
std::cout << "After move, str is \"" << str << "\"\n";
}
输出是
str.data() before move: 0x3ec3048
============================
str.data() after push_back: 0x3ec3048
v[0].data() after push_back: 0x3ec3048
============================
str.data() after move: 0x601df8
v[0].data() after move: 0x3ec3048
v[1].data() after move: 0x3ec3048
After move, str is ""
============================
str.data() after move: 0x3ec3048
v[0].data() after move: 0x3ec3048
v[1].data() after move: 0x601df8
After move, str is "Hello"
如果你看一下:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string str = "Hello";
std::vector<std::string> v;
std::cout << "str.data() before move: "<< static_cast<const void*>(str.data()) << std::endl;
v.push_back(str);
std::cout << "============================" << std::endl;
str[0] = 't';
std::cout << "str.data() after push_back: "<< static_cast<const void*>(str.data()) << std::endl;
std::cout << "v[0].data() after push_back: "<< static_cast<const void*>(v[0].data()) << std::endl;
}
然后你会假设这str[0] = 't'
只会替换数据。但这不一定是http://cpp.sh/47nsy的情况。
str.data() before move: 0x40b8258
============================
str.data() after push_back: 0x40b82a8
v[0].data() after push_back: 0x40b8258
并移动原语,例如:
void test(int i) {
int x=i;
int y=std::move(x);
std::cout<<x;
std::cout<<y;
}
大部分将由编译器完全优化:
mov ebx, edi
mov edi, offset std::cout
mov esi, ebx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov edi, offset std::cout
mov esi, ebx
pop rbx
jmp std::basic_ostream<char, std::char_traits<char> >::operator<<(int) # TAILCALL
两者都std::cout
使用相同的寄存器,x
并且y
完全优化了。
推荐阅读
- ruby-on-rails - ArgumentError ( is not a valid ) by enum
- reactjs - 如何导航到另一个页面?
- wordpress - 如何从 wordpress 插件中替换功能?
- node.js - Supabase:如何通过电子邮件查询用户?
- awk - 用其他命令修改 awk 参数
- apache-kafka - JHipster 与 kafka 真实应用使用示例
- java - 为什么要输出映射键,你需要在 forEach 之前使用 map (Map.Entry::getKey) ,而 forEach 中的 getKey () 不会给出相同的结果?
- javascript - 当文本被突出显示时,谷歌翻译扩展如何使其图标图像可见?
- ios - 如何将参数传递给快速调用转义函数的转义函数?
- css - 如何在 Svelte 中的组件内设置身体背景图像?