不合理删除的移动构造函数
Unreasonably deleted move constructor
我有一个 class,带有明确删除的复制构造函数,名为 NonCopyable
。然后是 class Base1
类型的成员 NonCopyable
。另一个 class Base2
以 Base1
作为父级。最后 - Derivative
class 父级是 Base2
。
由于 NonCopyable
是不可复制的,显然 Base1
、Base2
和 Derivative
也是不可复制的。但似乎也删除了移动构造函数(和赋值运算符)。
在这里测试:
下一行给出了这些错误:
Derived d1, d2 = std::move(d1);
GCC: 'Derived::Derived(Derived&&)' is implicitly deleted because the default definition would be ill-formed:
class Derived :
the rest of errors just claims that Base1
and Base2
copy ctors are implicitly deleted which is not weird
MSVC: error C2280: 'Derived::Derived(const Derived &)': attempting to reference a deleted function
在提供的 links 中,还有注释行给出了类似的错误。请取消注释它们以查看我想向您展示的更多错误(例如 remove_if
未注释它抱怨删除的移动 (在 GCC 上) /copy (在 MSVC 上) 赋值运算符).
我想要实现的是使 Derivative (及其基础)可移动,但不可复制。
我正在使用 Visual Studio 2015 并得到与我提供的 msvc link 完全相同的错误,但我不确定使用哪个版本的 MSVC 在 rextester.com。
应@Richard Critten 的要求,我也将代码粘贴在这里:
class NonCopyable
{
public:
NonCopyable() { }
NonCopyable(const NonCopyable &) = delete;
NonCopyable & operator=(const NonCopyable &) = delete;
NonCopyable(NonCopyable &&) { }
NonCopyable & operator=(NonCopyable &&) { return *this; }
};
class Base1
{
public:
virtual ~Base1() = default;
private:
NonCopyable m;
};
class Base2 :
public Base1
{
public:
virtual ~Base2() = default;
};
class Derived :
public Base2
{
};
int main()
{
std::vector<Derived> v;
//std::remove_if(v.begin(), v.end(), [](const Derived &) { return true; });
//v.emplace_back();
Derived d1, d2 = std::move(d1);
}
[class.copy]/9 If the definition of a class X
does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if
(9.4) — X
does not have a user-declared destructor.
Base1
有一个用户声明的析构函数,因此没有移动构造函数。由于不可复制的成员,复制构造函数被隐式声明为已删除。所以Base1
既不能复制也不能移动,当然还有Base2
和Derived
。
我有一个 class,带有明确删除的复制构造函数,名为 NonCopyable
。然后是 class Base1
类型的成员 NonCopyable
。另一个 class Base2
以 Base1
作为父级。最后 - Derivative
class 父级是 Base2
。
由于 NonCopyable
是不可复制的,显然 Base1
、Base2
和 Derivative
也是不可复制的。但似乎也删除了移动构造函数(和赋值运算符)。
在这里测试:
下一行给出了这些错误:
Derived d1, d2 = std::move(d1);
GCC: 'Derived::Derived(Derived&&)' is implicitly deleted because the default definition would be ill-formed: class Derived :
the rest of errors just claims thatBase1
andBase2
copy ctors are implicitly deleted which is not weirdMSVC: error C2280: 'Derived::Derived(const Derived &)': attempting to reference a deleted function
在提供的 links 中,还有注释行给出了类似的错误。请取消注释它们以查看我想向您展示的更多错误(例如 remove_if
未注释它抱怨删除的移动 (在 GCC 上) /copy (在 MSVC 上) 赋值运算符).
我想要实现的是使 Derivative (及其基础)可移动,但不可复制。
我正在使用 Visual Studio 2015 并得到与我提供的 msvc link 完全相同的错误,但我不确定使用哪个版本的 MSVC 在 rextester.com。
应@Richard Critten 的要求,我也将代码粘贴在这里:
class NonCopyable
{
public:
NonCopyable() { }
NonCopyable(const NonCopyable &) = delete;
NonCopyable & operator=(const NonCopyable &) = delete;
NonCopyable(NonCopyable &&) { }
NonCopyable & operator=(NonCopyable &&) { return *this; }
};
class Base1
{
public:
virtual ~Base1() = default;
private:
NonCopyable m;
};
class Base2 :
public Base1
{
public:
virtual ~Base2() = default;
};
class Derived :
public Base2
{
};
int main()
{
std::vector<Derived> v;
//std::remove_if(v.begin(), v.end(), [](const Derived &) { return true; });
//v.emplace_back();
Derived d1, d2 = std::move(d1);
}
[class.copy]/9 If the definition of a class
X
does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if(9.4) —
X
does not have a user-declared destructor.
Base1
有一个用户声明的析构函数,因此没有移动构造函数。由于不可复制的成员,复制构造函数被隐式声明为已删除。所以Base1
既不能复制也不能移动,当然还有Base2
和Derived
。