什么是std :: move(),什么时候应该使用它? [英] What is std::move(), and when should it be used?

查看:2161
本文介绍了什么是std :: move(),什么时候应该使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 是什么?

  • 它是什么?

  • >

    良好的链接。

    解决方案

    http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors < br>
    http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics


    1. 在C + +11,除了复制构造函数,对象可以有移动构造函数。

      (除了复制赋值运算符,它们还有移动赋值操作符。)

    2. 如果对象具有类型rvalue-reference( Type&& ),则使用move构造函数而不是复制构造函数。

    3. std :: move()是一个转换,它生成对对象的右值引用,以允许从中移动。



      1. 这是一种新的C ++方法来避免拷贝。例如,使用移动构造函数, std :: vector 可能只是将其内部指针复制到新对象的数据,使移动的对象处于不正确的状态,避免复制所有数据。这将是C ++ - 有效。



        尝试Google搜索移动语义,右值,完美转发。


        1. What is it?
        2. What does it do?
        3. When should it be used?

        Good links are appreciated.

        解决方案

        http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors
        http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics

        1. In C++11, in addition to copy constructors, objects can have move constructors.
          (And in addition to copy assignment operators, they have move assignment operators.)
        2. The move constructor is used instead of the copy constructor, if the object has type "rvalue-reference" (Type &&).
        3. std::move() is a cast that produces an rvalue-reference to an object, to enable moving from it.

        It's a new C++ way to avoid copies. For example, using a move constructor, a std::vector could just copy its internal pointer to data to the new object, leaving the moved object in an incorrect state, avoiding to copy all data. This would be C++-valid.

        Try googling for move semantics, rvalue, perfect forwarding.

        这篇关于什么是std :: move(),什么时候应该使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆