Some curious facts about C++

I am reading and learning about C++ and rvalue references and move semantics and stuff like that. Here are some things that took me by surprise.

Some rvalues can appear on the left side of an assignment operator. This, in hindsight, is less surprising than it looks. Let us say TClass is a class, and f() returns an object (by value) of type TClass. Then, we are used to calling f().someFunctionOnTClass() all the time. Assignment is another function called on the object, so it is like f().operator=(otherObject). It is kind of silly to call it, since the rvalue f() is temporary and changing it does not give us anything (we cannot use the changes).

Next one is less surprising, but interesting nonetheless. Some lvalues cannot appear on the left hand side of the assignment operator. What is the example? A couple:

  • A user defined type (TClass say) that does not have operator=() defined.
  • An array.