I was reading through Bjarne Stroustrup’s The C++ Programming Language, supposedly the definitive book on the C++ language by the man who invented it, when I found a mistake in it.
In Section 7.2.2 (in the chapter on operator overloading), he says:
In particular,
operator=,operator[],operator(), andoperator->must be non-static member functions; this ensures that their first operands will be lvalues.
That’s not true at all. Being member functions won’t ensure that their first operands are lvalues. And nor do the first operands even need to be lvalues. (I’m sure we’ve all written code like get_smart_pointer()->foo(), where get_smart_pointer returns an object of class type. In that situation, the first operand of the -> operator is not an lvalue.)
I suspect the real reason that operator[], operator(), and operator-> must be member functions is to prevent the first operands from being built-in types, and the reason operator= must be a member function is because the compiler needs to know at the time of the class’s definition whether or not to generate a default operator= function.
Apparently there are parts of the C++ language that Bjarne doesn’t actually understand.
(By the way, I checked two different editions of the book — an older one and a more recent one — and they both contain this error. So it wasn’t just a one-off misprint.)




