考虑以下代码片段,其中我有一个只能 move 的 Wrapper
对象,该对象将参数转发给构造时的基础数据。现在,如果我在另一个仅 move 类(在本例中为 SomeObject
)中使用此 Wrapper
对象,除非 SomeObject
的 move 构造函数是默认的,它无法编译。
#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;
}
在 gcc 6.3 中我们得到:
test.cpp: In instantiation of ‘Wrapper<T>::Wrapper(Wrapper<T>&&) [with T = int]’:
test.cpp:20:56: required from here
test.cpp:10:52: error: cannot convert ‘std::remove_reference<Wrapper<int>&>::type {aka Wrapper<int>}’ to ‘int’ in initialization
Wrapper(Wrapper &&other) : _data(std::move(other)) {}
^
我在这里遗漏了什么吗?用户在 SomeObject
中提供的 move 构造函数不是与编译器在默认情况下定义的 move 构造函数相同吗?
我能找到的唯一接近这个的是this answer但我觉得这是一个不同的情况,因为我的 SomeObject
move 构造函数没有传递 const
类型。
最佳答案
这是预料之中的。查看您的代码:
Wrapper(Wrapper &&other) : _data(std::move(other)) {}
T _data;
在你的例子中,T 是整数。你想怎么初始化int
来自 Wrapper<int>
?
关于c++ - 可变参数构造函数优先于用户提供的 move 构造函数,默认情况下除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46613777/