移动构造函数和继承 [英] Move constructors and inheritance

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

问题描述

我试图理解在C ++ 11中移动构造函数和赋值操作的方式,但是在将其委托给父类时遇到了问题。

I am trying to understand the way move constructors and assignment ops work in C++11 but I'm having problems with delegating to parent classes.

代码:

class T0
{
public:
    T0() { puts("ctor 0"); }
    ~T0() { puts("dtor 0"); }
    T0(T0 const&) { puts("copy 0"); }
    T0(T0&&) { puts("move 0"); }
    T0& operator=(T0 const&) { puts("assign 0"); return *this; }
    T0& operator=(T0&&) { puts("move assign 0"); return *this; }
};

class T : public T0
{
public:
    T(): T0() { puts("ctor"); }
    ~T() { puts("dtor"); }
    T(T const& o): T0(o) { puts("copy"); }
    T(T&& o): T0(o) { puts("move"); }
    T& operator=(T const& o) { puts("assign"); return static_cast<T&>(T0::operator=(o)); }
    T& operator=(T&& o) { puts("move assign"); return static_cast<T&>(T0::operator=(o)); }
};

int main()
{
    T t = std::move(T());
    return 0;
}

但是,当我在VS2012下编译并运行时,输出指示左值T0成员的版本称为:

However, when I compile and run under VS2012, the output indicates that the lvalue versions of the T0 members are called:

ctor 0
ctor
copy 0  <--
move    <--
dtor
dtor 0
dtor
dtor 0

移动分配也会发生类似的情况(测试用例略有不同)– T的移动分配运算符将T0称为正常分配运算符。

A similar situation (with a slightly different test case) happens with move assignments -- the move assignment operator of T calls the "normal" assignment operator of T0.

我在做什么错了?

推荐答案

关于将右值引用用作函数的更令人困惑的事情之一参数是在内部将参数视为左值。这是为了防止您在意图之前移动参数,但这需要一些时间来习惯。为了实际移动参数,您必须在其上调用std :: move(或std :: forward)。因此,您需要将移动构造函数定义为:

One of the more confusing things about functions taking rvalue references as parameters is that internally they treat their parameters as lvalues. This is to prevent you from moving the parameter before you mean to, but it takes some getting used to. In order to actually move the parameter, you have to call std::move (or std::forward) on it. So you need to define your move constructor as:

T(T&& o): T0(std::move(o)) { puts("move"); }

和您的移动分配运算符为:

and your move assignment operator as:

T& operator=(T&& o) { puts("move assign"); return static_cast<T&>(T0::operator=(std::move(o))); }

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

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