代码之家  ›  专栏  ›  技术社区  ›  Roger Pate

如何使用移动的对象?[复制品]

  •  8
  • Roger Pate  · 技术社区  · 14 年前

    这个问题已经有了答案:

    移动对象后,它必须是可销毁的:

    T obj;
    func(std::move(obj));
    // don't use obj and let it be destroyed as normal
    

    但是对于obj还能做什么呢?你能再移一个物体进去吗?

    T obj;
    func(std::move(obj));
    obj = std::move(other);
    

    这取决于具体的类型吗?(例如,std::vector可以做出您不能完全依赖的特定保证。)除了对从对象上移动的对象进行销毁之外,所有类型是否都支持某些东西是必需的,甚至是健全的?

    5 回复  |  直到 14 年前
        1
  •  6
  •   Henrik    14 年前

        2
  •  6
  •   Anthony Williams    14 年前

    size() empty()

        3
  •  1
  •   Å imon Tóth    14 年前

        4
  •  1
  •   Alex F    14 年前

    SomeClass::SomeClass(SomeClass&& v)
    {
        // Inside of this function, v is not rvalue anymore. But I know that actually 
        // this is rvalue, and use std::move
        OtherFunction(std::move(v));
    }