For lack of anything else to write about, here’s an amusing bug that I found the other day. Consider the following fragment of C++ code.

if (ProbeThing(address),true) {
    HandleDuplicateThing(address);
}

I was hitting HandleDuplicateThing when I thought I shouldn’t be. I went into the debugger and saw that, sure enough, ProbeThing was getting the correct/expected null value, but somehow when I got back into the caller it was still going down the wrong path. The problem turned out to be the placement of a right paren. What I meant to call was this:

if (ProbeThing(address,true)) {

See the difference? It turns out that the second argument to ProbeThing was optional, so the compiler didn’t see anything wrong with the incorrect call, and just applied the never-sufficiently-damned comma operator which would return its right half (true) as the value of the expression. Optional arguments are handy, but combined with the comma operator they can be very dangerous. So my question is this: who ever thought allowing comma as an operator was a good idea? I’ve seen it cause plenty of problems, but the only case it ever seems to be useful is in for-loop headers. If that’s the only use for them, the comma operator is an unnecessary hack. Function argument lists already use the comma as a syntactic element instead of as an operator, and for-loop headers could do the same thing to satisfy existing usage without requiring the generally-dangerous comma operator. I think I’m going to make a “no comma” sign and post it in my cube.