One of the most often heard complaints about C++ is that the compiler does things behind the programmer’s back. Conversion operators are the example most often cited. There’s a story that Jerry Schwarz, the architect of the iostream library, tells about his first attempt to support a scalar test of an iostream class object such as
if ( cin ) ...
cin to evaluate to a true/false scalar value, Jerry first defined an
operator int() conversion operator. This worked fine in well-behaved instances such as this example, but it behaved in a somewhat surprising manner under the following programmer error:
// oops: meant cout, not cin cin << intVal;
The programmer, of course, meant
cin. The type-safe ...