Bjarne Stroustrup: Full of crap Very nice fellow

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(), and operator-> 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.)

2 Responses to “Bjarne Stroustrup: Full of crap Very nice fellow”

  1. Anonymous Coward Says:

    Any book has bugs, but this isn’t one. You just misunderstood: In C++, temporary objects of class type are converted to lvalues to call member functions on them, because the language needs to do that to get the this pointer.

    Why so quick to call someone you don’t personally know (and the creator of the thing you think you know better than he does) “full of crap,” instead of first looking to see if maybe there’s something he knows that you don’t understand yet? Suggestion: Ask Google to fact-check before posting, that’s where I found the answer in under 30 seconds.

    • Inscrutable Ted Says:

      No, I haven’t misunderstood. You’ve misunderstood.

      1. Objects do not have lvalue-ness or rvalue-ness; expressions do. So saying “temporary objects of class type are converted to lvalues” doesn’t makes sense.

      2. The word “operand” refers to the expressions “a” and “b” used within an expression like “a+b”. It has nothing to do with how the objects represented by “a” and “b” are accessed from the “operator+” function, whether through function parameters or via the ‘this’ pointer.

      Some built-in operators require their operands to be lvalues, like unary & and unary +=. But user-defined operators have no such requirement. So I don’t know why Bjarne said what he said. It isn’t true.

      3. Even if “operand” did refer to the function parameters of the “operator+” function and/or to the value represented by “*this” (as you think it does), I’d like to point out that _those are always lvalues_, regardless of whether “operator+” is a member function or a non-member function. So Bjarne’s statement would still make no sense.

      Also, I’m not being quick to call someone “full of crap”. I spent a lot of time trying to figure this out before I made my post. (Far longer than 30 seconds, I assure you).

      By the way, I don’t mean to attack Bjarne’s intelligence or knowledge. The C++ language is such a complex mess (designed by a huge committee) that I don’t think anyone knows it all completely. But this book cost me more than $100, and it’s been through numerous editions and revisions. I would have expected it to have been fact-checked better.


Leave a Reply