我试图通过一个简单的例子(如下)来理解 std::move 是如何进行的。 基本上我试图将 p1 的内容移至 p2,因此 p1 之后为空,但这不会发生。
我想我没有正确使用 std::move 。如果有人能向我解释这一点,我将不胜感激。
#include <iostream>
#include <utility>
int main()
{
int * p1 = new int[10];
for(int i = 0; i < 10; ++i)
p1[i]=i;
// moving contents of p1 to p2
int * p2 = std::move(p1);
// I was expeting p1 now to be empty but it's not...
if(p1 != NULL)
std::cout << "I'M NOT EMPTY\n";
// prints I'M NOT EMPTY
}
最佳答案
move 函数不保证清除源对象的内容。但是,保证它处于这样的状态,您可以安全地销毁它或为其分配新值。
关于c++ - 无法使用 std::move move 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15741626/