要删除 copy/move 赋值运算符的有效签名
Valid signature to delete copy/move assignment operator
使用以下签名(注意 void
作为结果类型)删除复制或移动赋值运算符(可能是唯一的)是否有效?
struct A
{
A() = default;
void operator = (const A &) = delete;
void operator = (A &) = delete;
void operator = (A &&) = delete;
};
G++/clang++ 接受上面的代码(实际上它可以被认为是缩短)。是否对赋值运算符的签名有更严格的要求才能明确删除?
继承有没有干扰?
完全正确。如果您的 class 不能 need/want/cannot 使用这些函数,那么明确说明并删除它们是正确的做法(即使 return 类型不参与重载决策,您也可以想让它们正确只是为了减少混淆)。
A user-declared copy assignment operator X::operator=
is a non-static non-template member function of class X
with exactly one parameter of type X
, X&
, const X&
, volatile X&
or const volatile X&
.
和 move 类似:
A user-declared move assignment operator X::operator=
is a non-static non-template member function of class X
with exactly one parameter of type X&&
, const X&&
, volatile X&&
, or const volatile X&&
.
这里对return类型没有限制。仅在参数类型上。编译器生成的这些版本 returns X&
,但这绝不会迫使您做同样的事情。
如果您无论如何都要删除赋值运算符,returning void
就可以了。请注意,您不需要同时删除复制和移动赋值运算符。如果删除复制赋值,则move won't be implicitly generated。
Is there any interference in case of inheritance?
没有。隐式生成的赋值运算符只是 defined as deleted(并且会是 return X&
,尽管这是正交的)。如果你声明一个,那当然是你想做什么就做什么。
使用以下签名(注意 void
作为结果类型)删除复制或移动赋值运算符(可能是唯一的)是否有效?
struct A
{
A() = default;
void operator = (const A &) = delete;
void operator = (A &) = delete;
void operator = (A &&) = delete;
};
G++/clang++ 接受上面的代码(实际上它可以被认为是缩短)。是否对赋值运算符的签名有更严格的要求才能明确删除?
继承有没有干扰?
完全正确。如果您的 class 不能 need/want/cannot 使用这些函数,那么明确说明并删除它们是正确的做法(即使 return 类型不参与重载决策,您也可以想让它们正确只是为了减少混淆)。
A user-declared copy assignment operator
X::operator=
is a non-static non-template member function of classX
with exactly one parameter of typeX
,X&
,const X&
,volatile X&
orconst volatile X&
.
和 move 类似:
A user-declared move assignment operator
X::operator=
is a non-static non-template member function of classX
with exactly one parameter of typeX&&
,const X&&
,volatile X&&
, orconst volatile X&&
.
这里对return类型没有限制。仅在参数类型上。编译器生成的这些版本 returns X&
,但这绝不会迫使您做同样的事情。
如果您无论如何都要删除赋值运算符,returning void
就可以了。请注意,您不需要同时删除复制和移动赋值运算符。如果删除复制赋值,则move won't be implicitly generated。
Is there any interference in case of inheritance?
没有。隐式生成的赋值运算符只是 defined as deleted(并且会是 return X&
,尽管这是正交的)。如果你声明一个,那当然是你想做什么就做什么。