用户定义的移动构造函数,用于左值引用类型的成员

时间:2015-01-08 23:12:45

标签: c++ c++11 move-semantics

我在编译器上使用移动语义,它有rvalue引用但不支持默认的移动构造函数。我想生成类似下面的包装类,即使模板参数是左值引用也可以。但是,这种简单的方法无法编译,因为它尝试初始化int&来自int。

#define USER_DEFINED   0

template <typename T>
struct Wrapper
{
    Wrapper(T t)
        : m_t(t)
    {
    }

    Wrapper(const Wrapper&) = delete;
    Wrapper& operator=(const Wrapper&) = delete;

#if USER_DEFINED
    Wrapper(Wrapper&& w)
        : m_t(std::move(w.m_t))
    {
    }
#else
    Wrapper(Wrapper&&) = default;
#endif

private:
    T m_t;
};

int main()
{
    int i = 0;
    Wrapper<int&> w1 = Wrapper<int&>(i);
    Wrapper<std::string> w2 = Wrapper<std::string>("text");
}

显而易见的解决方案是拥有两个移动构造函数,一个用于左值引用,另一个用于所有其他类型。像这样的东西:

template <typename U = T>
Wrapper(typename std::enable_if<!std::is_lvalue_reference<U>::value, Wrapper>::type&& w)
    : m_t(std::move(w.m_t))
{
}

template <typename U = T>
Wrapper(typename std::enable_if<std::is_lvalue_reference<U>::value, Wrapper>::type&& w)
    : m_t(w.m_t)
{
}

这是要走的路吗?也许enable_if<>中的表达式应该更通用?或者我可以使用与std :: move()不同的东西,并为所有类型使用一个构造函数吗?

1 个答案:

答案 0 :(得分:4)

好的,这是一个我认为可以按你的需要工作的解决方案,但我必须承认我并不完全理解它是如何工作的。

我所做的最重要的改变是在移动构造函数中用std::forward<T>替换std::move。我还添加了一个移动赋值运算符,但这是我不明白的事情:除非在其他地方,它需要std::move而不是std::forward!最后,我还在您的构造函数中添加了一个std::forward,它接受​​T,因此它不会生成两个的参数副本。实际上我们需要按值接受T并在此处使用std::forward。如果const T&是引用,那么T&&T的重载会失败,因为T&&也会collapse到左值引用,并且重载变得模棱两可。

#include <iostream>
#include <utility>

template <typename T>
class Wrapper
{

public:

  Wrapper(T t) : m_t {std::forward<T>(t)}
  {
  }

  Wrapper(Wrapper&& w) : m_t {std::forward<T>(w.m_t)}
  {
  }

  Wrapper(const Wrapper&) = delete;

  Wrapper&
  operator=(Wrapper&& w)
  {
    if (this != &w)
      this->m_t = std::move(w.m_t);
    return *this;
  }

  Wrapper&
  operator=(const Wrapper&) = delete;

private:

  T m_t;
};

现在,让我们带一个带有检测类型的试驾,让我们看看发生了什么。

struct A
{
  A ()
  {
    std::cerr << "A was default-constructed" << std::endl;
  }

  A (const A&)
  {
    std::cerr << "A was copy-constructed" << std::endl;
  }

  A (A&&)
  {
    std::cerr << "A was move-constructed" << std::endl;
  }

  A&
  operator=(const A&)
  {
    std::cerr << "A was copy-assigned" << std::endl;
    return *this;
  }

  A&
  operator=(A&&)
  {
    std::cerr << "A was move-assigned" << std::endl;
    return *this;
  }

  ~A ()
  {
    std::cerr << "A was destroyed" << std::endl;
  }
};

int main()
{
  A a {};
  Wrapper<A> val1 {a};
  Wrapper<A> val2 {std::move(val1)};
  val1 = std::move(val2);
  Wrapper<A&> ref1 {a};
  Wrapper<A&> ref2 {std::move(ref1)};
  ref1 = std::move(ref2);
}

使用GCC 4.9.1进行编译(整个练习实际上是毫无意义的,因为它支持已开箱即用的所有类型的动作。)并关闭所有优化,输出如下(添加注释)。

A was default-constructed  ; automatic variable a in main
A was copy-constructed     ; copied as Wrapper<A>'s constructor argument
A was move-constructed     ; forwarded in Wrapper<A>'s initializer
A was destroyed            ; the moved-away from copy as the constructor returns
A was move-constructed     ; forwarded in Wrapper<A>'s move-constructor
A was move-assigned        ; move-assignment operator in Wrapper<A>
A was move-assigned        ; not sure about this one... (apparently caused by the Wrapper<A&> move-assignment)
A was destroyed            ; the sub-object of val2
A was destroyed            ; the sub-object of val1
A was destroyed            ; the automatic variable a in main