如何定义一个移动构造函数? [英] How to define a move constructor?

查看:271
本文介绍了如何定义一个移动构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual studio 11上尝试一些新的C ++ 11功能,从移动构造函数开始。我写了一个简单的类MyClass,包含一个移动构造函数:

  class MyClass 
{
public :
explicit MyClass(int aiCount)
:mpiSize(new int(aiCount)),
miSize2(aiCount)
{
}
$ b b MyClass(MyClass& rcOther)
:mpiSize(rcOther.mpiSize)
,miSize(* rcOther.mpiSize)
{
rcOther.mpiSize = 0;
rcOther.miSize = 0;
}

〜MyClass()
{
delete mpiSize;
}

private:
int * mpiSize;
int miSize2;

};

我在这里遇到了问题:


  1. 我假设编译器会为MyClass生成一个移动构造函数,如果我不实现一个 - 但它似乎不是吗?

  2. 是否有更好的方法来实现MyClass的移动构造函数?


解决方案


  1. MSVC ++在标准的最终版本之前实施移动构造函数不在。在标准MSVC ++的实现的基础上,生成默认移动构造函数的规则比在标准的最终版本中更严格。请参阅:为什么此代码尝试调用副本构造函数?(特别是此答案及其上的评论),了解更多信息。


  2. 不,你需要在 rcOther 的成员上调用 std :: move ,并且用相应的成员初始化成员从你的错误的对象(你错误的 miSize ):

      MyClass ;& rcOther)
    :mpiSize(std :: move(rcOther.mpiSize))
    ,miSize2(std :: move(rcOther.miSize2))
    {
    rcOther。 mpiSize = 0;
    }

    对于 int int * ,但是肯定会对用户定义的类型产生影响。


    • 这样做的原因是 std :: move 只是返回casted into一个 T&& ,一个右值引用,使正确的构造函数(move constructor, T(T&& c $ c>)为每个子对象调用。如果你没有在死亡对象的成员上使用 std :: move ,它们将被视为 T& ,并且将调用子对象( T(T&))的复制构造函数,而不是move构造函数。这是非常糟糕的,阻碍了几乎你写了一个移动构造函数的整个目的。




3.您正在做一些不必要的事情,例如将整数设置为0.您只需要将指针设置为0,以便 delete 它不会删除您创建的新对象的资源。



此外,如果这不是一个教学练习,请考虑使用 std :: unique_ptr ,而不是管理自己的对象的生命周期。这样你甚至不必为你的类写一个析构函数。注意,如果你这样做,那么使用 std :: move 从移动构造函数中的垂死成员初始化成员是必须的。







  • Konrad Rudolph在他的回答中发现你的类管理一个非自动资源,但不遵循 > 五次三,四,五的规则。有关详情,请参阅他的答案。


I am trying some new C++11 features on visual studio 11, started with the move constructor. I wrote a simple class called "MyClass" containing a move constructor:

class MyClass
{
public:
    explicit MyClass( int aiCount ) 
        : mpiSize( new int( aiCount ) ),
          miSize2( aiCount)
    {
    }

    MyClass( MyClass&& rcOther )
        : mpiSize( rcOther.mpiSize )
        , miSize( *rcOther.mpiSize )
    {
       rcOther.mpiSize = 0;
       rcOther.miSize = 0;
    }

    ~MyClass() 
    {
        delete mpiSize;
    }

private:
    int *mpiSize;
    int miSize2;

};

I got there questions here:

  1. I assumed that the compiler would generate a move constructor for MyClass if I don't implement one - but it doesn't seems so?
  2. Is the implementation of the move constructor correct for MyClass?
  3. Is there a better way to implement the move constructor for MyClass?

解决方案

  1. MSVC++ implemented move constructors before the final version of the standard was out. In the version of the standard MSVC++'s implementation was based on, the rules for generating a default move constructor were ridiculously more strict than they are in the final version of the standard. See here: Why is this code trying to call the copy constructor? (specifically this answer and the comments on it) for more info on that. This has not been and will not be fixed in Visual Studio 11, for some unknown stupid reason because they had other priorities.

  2. No, you need to call std::move on the members of rcOther, and you initialise members with the corresponding members from the dying object (you misnamed miSize):

    MyClass( MyClass&& rcOther )
        : mpiSize( std::move(rcOther.mpiSize) )
        , miSize2( std::move(rcOther.miSize2) )
    {
       rcOther.mpiSize = 0;
    }
    

    It doesn't make a difference for built in types like int and int*, but it definitely makes a difference for user-defined types.

    • The reason for this is that std::move just returns the argument casted into a T&&, an rvalue-reference, so that the correct constructor (the move constructor, T(T&&)) is called for each of the sub-objects. If you don't use std::move on the members of the dying object, they will be treated like T&, and the copy constructor of your subobjects (T(T&)) will be called instead of the move constructor. That is very bad and thwarts almost the entire purpose of you having written a move constructor.


3. You are doing some unnecessary things, like setting the integer to 0. You only need to set the pointer to 0 so that deleteing it won't delete the resource of the new object you created.

Also, if this is not a didactic exercise, you may want to consider using std::unique_ptr instead of managing the lifetime of your own object. This way you don't even have to write a destructor for your class. Note that if you do that, using std::move to initialise the member from the dying member in the move constructor would be mandatory.


  • Konrad Rudolph in his answer has caught the fact that your class manages a non-automatic resource but does not follow the Rule of Five Three, Four, or Five. See his answer for more details on this.

这篇关于如何定义一个移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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