tags:

views:

411

answers:

2

If I define an own assignment operator, which has a different signature than the normally generated default assignment operator:

struct B;
struct A {
void operator = (const B& b) {
    // assign something
}
};

does the default assignment operator, in this case operator = (A&) (or the like, correct me if wrong) become undefined/unaccessible?

AFAIK this is true for the default constructor, which doesn't exist, if we define some other constructor. But I am really not sure if this is the case for the other "magic" defaults.

The reason I ask: I want to avoid that the default copy constructor is accidently called via a implicit type conversion. If it doesn't exist, it could never happen.

+4  A: 

Since A& operator=( B& ) has not the signature of A& operator=( const A& ), this does nothing to the synthesized assignement operator.

Take a look at this snippet at codepad.org - as far as an example counts as proof.

Test driving Comeau with it also shows that A& operator=( const A& ) is synthesized.

xtofl
Very interesting example. I will make the default copy assignment operator private to be sure.
drhirsch
+12  A: 

No. 12.8/9 says that the assignment operator for class X must be non-static, non-template with a parameter of type X, X&, X const&, X volatile& or X const volatile&. And there is a note which emphasizes that the instantiation of a template doesn't suppress the implicit declaration.

AProgrammer
Thanks for the standard reference! That actually says something more than my examples.
xtofl
You are right. I'll delete my answer.
anon
@Neil, you wondered of what use =default could be. One is to be explicit in what is available, another is to change the accessibility a third is that it allows to make the implementation not inline while ensuring that evolution of the class doesn't break it. Another justification is that the feature is wider is scope than assignment and there was no incentive to make an exception even if it had be of no use.
AProgrammer
Thanks - that was very useful. What I really like about SO is not so much answering questions, gaining rep etc, but learning new stuff!
anon