C++ 除默认情况外,可变构造函数优先于用户提供的移动构造函数

C++ 除默认情况外,可变构造函数优先于用户提供的移动构造函数,c++,c++11,templates,variadic-templates,move-semantics,C++,C++11,Templates,Variadic Templates,Move Semantics,考虑下面的代码片段,其中我有一个move-only包装器对象,它将参数转发到构建时的底层数据。现在,如果我在另一个仅移动类(SomeObject)中使用这个Wrapper对象,除非SomeObject的移动构造函数是默认的,否则它不会编译 #include <utility> template <typename T> class Wrapper { public: template <typename... Args> Wrapper(Args &

考虑下面的代码片段,其中我有一个move-only
包装器
对象,它将参数转发到构建时的底层数据。现在,如果我在另一个仅移动类(
SomeObject
)中使用这个
Wrapper
对象,除非
SomeObject
的移动构造函数是默认的,否则它不会编译

#include <utility>

template <typename T> class Wrapper {
public:
  template <typename... Args>
  Wrapper(Args &&... args) : _data{std::forward<Args>(args)...} {}

  Wrapper(const Wrapper &) = delete;

  Wrapper(Wrapper &&other) : _data(std::move(other)) {}

  T &get() const { return _data; }

protected:
  T _data;
};

struct SomeObject {
  SomeObject(const SomeObject &) = delete;
  SomeObject(SomeObject &&other) : x(std::move(other.x)) {}
  //SomeObject(SomeObject &&other) = default; // this works!
  SomeObject(int val) : x(val) {}

  Wrapper<int> x;
};

int main() {
  SomeObject obj(10);
  return 0;
}
#包括
模板类包装器{
公众:
样板

包装器(Args&…Args):\u data{std::forward,但我觉得这是另一种情况,因为我的
SomeObject
的move构造函数没有传递
const
类型。

这是预期的。请查看您的代码:

Wrapper(Wrapper &&other) : _data(std::move(other)) {}
T _data;

在您的例子中,T是int。您想如何从
包装器
初始化
int

使用
Wrapper(包装器和其他):\u数据(std::move(other.\u数据)){}
更明确地说,将
std::move(other)
更改为
std::move(other.\u数据)
Wrapper(Wrapper &&other) : _data(std::move(other)) {}
T _data;