0

Possible Duplicate:
behaviour of the implicit copy constructor / assignment operator
C++ - conditions for automatic generation of default ctor, copy ctor,and default assignment operator?

Is it true that the overloaded assignment operator is always provided by the C++ compiler? What are the cases in which it is not provided by the c++ compiler?

Community
  • 1
  • 1
Raulp
  • 7,758
  • 20
  • 93
  • 155

2 Answers2

2

An Assignment/Copy Assignment(=) operator is provided by any C++ compiler implicitly unless you have const or reference members in your class.

In case of const member compiler cannot provide = because that would break the contract of not modifying const member after initialization.

In case of reference member compiler does not provide = because it leaves it to the user of the class to decide the appropriate behavior.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • @OliCharlesworth why so? – Raulp Jun 07 '12 at 18:02
  • 1
    @softy: Updated to explain *Why?* – Alok Save Jun 07 '12 at 18:03
  • 1
    Or if the class has members which themselves do not have publicly accessible assignment operators. – Benjamin Lindley Jun 07 '12 at 18:04
  • 1
    Anyone feels inclined to share why the downvote? I am fed up of asking people *Why?* if you don't share a reason why, your downvote gets wasted because no one takes it seriously especially on an accepted answer. So grow up from your childhood lull and pray tell *Why?* – Alok Save Jun 07 '12 at 18:21
  • @Als: Your first sentence is wrong(incomplete), my previous comment explains why. I always downvote answers that have incorrect information, don't take it personally. – Benjamin Lindley Jun 07 '12 at 18:22
  • @BenjaminLindley: So you have added it as an comment to complete it and I will let it be, free feel to add it as an answer and I will upvote it. – Alok Save Jun 07 '12 at 18:23
  • Technically the assignment operator will be implicitly declared *always* (unless you declare your own), and if it is used, the it will also be implicitly defined, which means that the answer is **yes** it will be generated by the compiler. Now, if the generated assignment is incorrect it will fail to compile, but that is different from not having an assignment operator. – David Rodríguez - dribeas Jun 07 '12 at 18:25
1

Is it true that the overloaded assignment operator is always provided by the C++ compiler.

If you don't declare your own assignment operator, the compiler will implicitly declare one for you (always). If you use it, then the compiler will also implicitly define it (always).

There is a common misunderstanding shown in Als's answer that it will not be defined under some conditions. That is wrong, whether the type has constant members or references does not matter at all, the compiler will declare and define it. The situation for those cases is that the implicitly defined compiler will fail to compile.

If you try that in a compiler, the error will not point to a missing assignment operator, but rather to an error while compiling the implicitly defined assignment.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • I am not sure *"compiler will always provide a implicit definition."* is correct.The diagnostic error message in this [useless example](http://ideone.com/9atuj) just says so but I am not sure if it actually does.It is not conclusive enough.Does the standard explicitly mention that the definition be provided in all scenarios? Especially even in cases where a compiler can detect much in advance that it cannot provide a compilable definition? – Alok Save Jun 09 '12 at 07:47
  • @Als: 12.8p26 *A copy/move assignment operator that is defaulted and not defined as deleted is implicitly defined when it is odr-used* – David Rodríguez - dribeas Jun 09 '12 at 11:23
  • Thanks, that confirms.@softy, Please unaccept my answer and mark David's answer as accepted one.My answer tells you the scenarios where you will be **forced to** provide your own assignment operator but your Q was *Is the assignment operator always provided*, the answer as David's answer points out it is **Yes**, whether it is usable or not is a different Q which is what my answer talks about. – Alok Save Jun 09 '12 at 12:03