c++ - 无法使用 std::move move 内容

标签 c++ std move move-semantics

我试图通过一个简单的例子(如下)来理解 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/

相关文章:

c++ - 需要有关 VNC 类应用程序中的图 block 缓存机制的建议

c++ - Qt QProcess 读取 ssh 详细输出

c++ - 命名空间参数的理想架构设计

c++ - 在 C++ 中使用 RAII 在线程包装类中 move 语义

swift - 如何在 swift 约束上使用 if else 语句

html - CSS - 悬停时文本 move

c++ - 实践中的无限制 union

c++ - 检查两个 std::function 是否相等

c++ - 将 2 维 Char[][] 数组的特定范围复制到 char * 或 std::string

c++ - 在 VS2013 中初始化 std::discrete_distribution