我有以下代码:
template <typename T>
struct X {
X() : x(5) {}
template <template <typename> class S>
X(const S<T> & y) : x(y.x) {}
// Uncomment and it does not compile anymore
// X(X&& z) : x(z.x) {}
T x;
};
int main ()
{
X<int> y;
auto x = y;
return 0;
}
您能解释一下为什么在取消注释指定代码后它无法编译吗?
最佳答案
复制构造函数被隐式声明为已删除,因为您声明了一个移动构造函数。
添加一个默认的(或用户定义的)复制构造函数,它可以正常编译:
X(const X<T>&)=default;
关于C++ 不会通过取消注释移动运算符来编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38414851/