An interesting quote from (the very quotable) The Design and Evolution of C++ by Bjarne Stroustrup (emphasis added):
My impression was and is that many programming languages and tools represent solutions looking for problems, and I was determined that my work should not fall into that category. Thus, I follow the literature on programming languages and the debates about programming languages primarily looking for ideas for solutions to problems my colleagues and I have encountered in real applications. Other programming languages constitute a mountain of ideas and inspiration — but it has to be mined carefully to avoid featurism and inconsistencies. The main sources for ideas for C++ were Simula, Algol68, and later Clu, Ada, and ML. The key to good design is insight into problems, not the provision of the most advanced features.
Also, a C bug-hunt trivia question. Can you spot the logic error in this C code I recently saw?
int i;
int bool = 1; /* assume success unless function returns 0 */
for (i=0; i < numdevices; ++i)
bool &= function_returning_number_of_records_processed(i);
if (!bool)
error("blah blah");
Hint: in the simplest case, the number of records processed can be either 0 (an error), 1, or 2.
Stab, stab, stab to the programmer. The answer appears in the comments.
The short answer: the programmer was trying to do a C++-ish thing (use a boolean-value variable) but botched it:
intis notbool.The long answer. The
&=operation is a bitwise or, not a logical (boolean) one. If the function returned the value 2 (or any even value), bool would be set to2 & 1, which is 0, erroneously indicating an error when there was none. I believe the original programmer was thinking of2 && 1, which would of course work correctly. Alas, there is no&&=operator in C or C++.Comment by jon — 2006.Jul.9 @ 12:47