How are C++ array members handled in copy control functions?

This is what the standard says in 12.8 (Copying class objects). Copy construction: Each subobject is copied in the manner appropriate to its type: if the subobject is of class type, the copy constructor for the class is used; if the subobject is an array, each element is copied, in the manner appropriate to the … Read more

what is return type of assignment operator?

The standard correctly defines the return type of an assignment operator. Actually, the assignment operation itself doesn’t depend on the return value – that’s why the return type isn’t straightforward to understanding. The return type is important for chaining operations. Consider the following construction: a = b = c;. This should be equal to a … Read more

const member and assignment operator. How to avoid the undefined behavior?

[*] Your code causes undefined behavior. Not just “undefined if A is used as a base class and this, that or the other”. Actually undefined, always. return *this is already UB, because this is not guaranteed to refer to the new object. Specifically, consider 3.8/7: If, after the lifetime of an object has ended and … Read more

Lua operators, why isn’t +=, -= and so on defined?

This is just guesswork on my part, but: 1. It’s hard to implement this in a single-pass compiler Lua’s bytecode compiler is implemented as a single-pass recursive descent parser that immediately generates code. It does not parse to a separate AST structure and then in a second pass convert that to bytecode. This forces some … Read more

Why don’t Java’s +=, -=, *=, /= compound assignment operators require casting long to int?

As always with these questions, the JLS holds the answer. In this case §15.26.2 Compound Assignment Operators. An extract: A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. An example cited from §15.26.2 […] the following code is … Read more

What is wrong with “checking for self-assignment” and what does it mean?

A question that’s of greater importance in this case would be: “What does it mean, when a you write function in a way, that requires you to check for self assignment???” To answer my rhetorical question: It means that a well-designed assignment operator should not need to check for self-assignment. Assigning an object to itself … Read more