In c++ 11, we could disable copy constructor and assignment operator, with delete:
class A {
A(const A&) = delete;
A& operator=(const A&) = delete;
}
One day, my colleague use the void return type rather than the reference.
class A {
A(const A&) = delete;
void operator=(const A&) = delete;
}
Is this one also ok?
e.g., if i have
A a, b, c;
a = b = c;
will this work?
Return types are not a part of the function signature in c++ (this is also why you can't overload functions only by the return type). So it's ok because your deleted function will still be found during the name lookup. You might have compiler warnings though, depending on your compiler version/settings.
The return type of operator=
is arbitrary. Returning a reference to *this
or void
is conventional. You could return int*
or std::string
. The conventional solution is usually better.
A deleted function cannot be called, and expressions deriving its return type are at the least substitution failures, so I see no standard-defined harm.
Compilers may or may not give worse diagnostics from the void return value than the reference one. Compare the a=b=c
case in the two cases for your compiler; noise about "cannot assign from void" would make me dislike returning void from the deleted version.
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