I need to disable the copy assignment operator. This will work:
A& operator=(const A&);
Will it work if I don't specify exact parameters for operator=
?
I mean something like this:
void operator=(void);
The returning value is right, I can write whatever I want, but what about the parameter type?
Will this override the default operator=
for class?
From 12.8p17 of the C++ standard draft:
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&
.
I guess this replies better than any other test or sample code.
Note that something similar applies also to the move assignment operator, see 12.8p19:
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&&.
These also confirm that, as you guessed, return value types don't matter.
There can be different kinds of assignments. Only copy assignment and move assignment are potentially generated by the compiler. They are generated if there is no copy/move assignment. So, if you want to disable copy and/or move assignment the argument type does matter although there is some flexibility as copy assignment can different argument types. The return type doesn't matter, though.
class A {
public:
void operator=() = delete; // not legal: assignment takes exactly one argument
void operator=(A) = delete; // OK: copy assignment disabled
void operator=(A&) = delete; // OK: copy assignment disabled
void operator=(A const&) = delete; // OK: copy assignment disabled
void operator=(A&&) = delete; // OK: move assignment disabled
};
There are also variations replacing const
by volatile
or const volatile
qualifying as copy/move assignments. When you disable a copy assignment, automatic generation of move assignment will be disabled, too. If you disable move assignment, I think copy assignment would still be generated. If you disable anything which cannot be a copy or move assignment, copy/move assignment are still generated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With