我熟悉这个原则(例如,从this answer和this one),当一个类有一个移动构造函数和/或移动赋值运算符时,它的默认复制构造函数和复制赋值运算符被删除。但是,在我看过的例子中,可以通过显式定义新的复制构造函数和赋值运算符来解决这个问题。
在我的特定情况下,我有一个类,它是通过C样式结构和模板类的联合继承派生的。复制和移动赋值运算符在模板中显式定义,而复制和移动构造函数在类本身中显式定义。换句话说,一切都是明确定义的,虽然不是全部在同一个地方。以下是一些示例代码:
typedef struct {
int n;
} myStruct;
template <typename T> class myTemplate
{
public:
// Default constructor
myTemplate<T>() : t_n(nullptr) {}
// Cannot create copy or move constructors in template, as cannot
// access the 'n' member directly
// Copy assignment operator
myTemplate<T> & operator=(const myTemplate<T> &source)
{
if (this != &source)
{
*t_n = *(source.t_n);
}
return *this;
}
//! Move assignment operator
myTemplate<T> & operator=(myTemplate<T> &&source)
{
if (this != &source)
{
*t_n = *(source.t_n);
*(source.t_n) = 0;
source.t_n = nullptr;
}
return *this;
}
T* t_n;
};
class myClass : public myStruct, public myTemplate<int>
{
public:
// Default constructor
myClass() : myTemplate<int>()
{
n = 0;
t_n = &n;
}
// Alternative constructor
myClass(const int &n_init) : myTemplate<int>()
{
n = n_init;
t_n = &n;
}
// Copy constructor
myClass(const myClass &source) : myTemplate<int>()
{
n = source.n;
t_n = &n;
}
// Move constructor
myClass(myClass &&source) : myTemplate<int>()
{
n = source.n;
t_n = &n;
source.n = 0;
source.t_n = nullptr;
}
};
int main()
{
myClass myObject(5);
myClass myOtherObject;
// Compilation error here:
myOtherObject = myObject;
return 1;
}
在Windows上的Visual C ++和英特尔C ++中,这完全符合我的预期。但是,在Linux中的gcc 4.9.0中,我收到了可怕的错误消息:
g++ -c -std=c++11 Main.cppMain.cpp: In function ‘int main()’:
Main.cpp:78:19: error: use of deleted function ‘myClass& myClass::operator=(const myClass&)’
myOtherObject = myObject;
^
Main.cpp:39:7: note: ‘myClass& myClass::operator=(const myClass&)’ is implicitly declared as deleted because ‘myClass’ declares a move constructor or move assignment operator
class myClass : public myStruct, public myTemplate<int>
果然,如果我在类本身而不是模板中定义一个显式的复制赋值运算符,那么错误就会消失,但这样做很麻烦并且破坏了使用模板的优势,因为(a)我的实际副本赋值运算符比这里显示的运算符大很多,并且(b)有大量不同的类都共享此模板。
那么,这只是gcc 4.9.0中的一个错误,或者这实际上是标准所说的应该发生的事情?
答案 0 :(得分:5)
GCC是正确的(Clang和EDG同意)。
myTemplate
具有用户声明的移动构造函数,因此会删除其复制赋值运算符。
您提供了复制构造函数,但没有提供复制赋值运算符。只需为myTemplate
声明一个副本赋值运算符,并将其定义为默认值。这需要一行额外的代码。