Search code examples
c++operator-overloadingassignment-operator

Does signature of custom assignment operator=() matter when I just want to disable it?


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?


Solution

  • 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 class X with exactly one parameter of type X, X&, const X&, volatile X& or const 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.