The fact is that for class types (but not for built-in types) a = b is short for a.operator=(b) , where operator = is a member function. And member functions can be called on r values, such (a * b) created by Rational::operator* . To apply similar semantics as for built-in rvalues ββ(" do as ints do "), some authors (including Meyers) recommended in C ++ 98 to return const-rvalue for classes with such operators.
However, in C ++ 11, returning const-rvalue is a bad idea, because it will interfere with the semantics of movement, because the rvalues ββconstants cannot bind to T&& .
In his notes Review of the New C ++ (C ++ 11) , Scott Meyers gives exactly the same example from his old book, and concludes that poor design is now considered to add a const value. Recommended Signature Now
Rational operator*(const Rational& lhs, const Rational& rhs);
UPDATE: As seen from @ JohannesSchaub-litb's comments, in C ++ 11 you can also use the assignment operator reference specifier so that it only accepts lvalues ββas its left argument (i.e. the *this pointer, so this the function is also known as "rvalue links for this"). You will need g ++> = 4.8.1 (just released) or Clang> = 2.9 to use it.
TemplateRex May 30 '13 at 11:30 2013-05-30 11:30
source share