[ Team LiB ] Previous Section Next Section

Gotcha #7: Ignorance of Base Language Subtleties

Most C++ programmers are confident that they're fully familiar with what might be considered the C++ "base language": that part of C++ inherited from C. However, even experienced C++ programmers are sometimes ignorant of the more abstruse details of these basic C/C++ statements and operators.

The logical operators are not what one would ordinarily consider abstruse, but they seem to be increasingly underutilized by new C++ programmers. Isn't it irritating to see code like this?



bool r = false; 


if( a < b )


   r = true;


Instead of this?



bool r = a<b; 


Do you have to count to eight when presented with the following?

gotcha07/bool.cpp



int ctr = 0; 


for( int i = 0; i < 8; ++i )


   if( options & 1<<(8+i) )


       if( ctr++ ) {


           cerr << "Too many options selected";


           break;


       }


Instead of this?

gotcha07/bool.cpp



typedef unsigned short Bits; 


inline Bits repeated( Bits b, Bits m )


   { return b & m & (b & m)-1; }


// . . .


if( repeated( options, 0XFF00 ) )


   cerr << "Too many options selected";


What ever happened to Boolean logic?

Likewise, many programmers are ignorant of the fact that the result of a conditional operator is an lvalue (see Gotcha #6) if both its potential results are lvalues. This ignorance necessitates code like the following:



// version #1 


if( a < b )


   a = val();


else if( b < c )


   b = val();


else


   c = val();


// version #2


a<b ? (a = val()) : b<c ? (b = val()) : (c = val());


An alternative solution with an lvalue conditional is definitely shorter and undeniably cooler:



// version #3 


(a<b?a:b<c?b:c) = val();


While this piece of esoteric knowledge may not seem as immediately relevant as a sound appreciation of Boolean logic, many contexts in C++ allow only expressions (constructor member-initialization-lists, throw-expressions, and so on).

Additionally, note that the call to the entity val occurs multiple times in versions #1 and #2, whereas it appears only once in version #3. If val is a function, this is of little importance. However, if val is a preprocessor macro, the presence of multiple expansions may produce incorrect side effects (see Gotcha #26). In these contexts, the availability of an effective conditional operator as a substitute for an if-statement can be essential. Effectively, while I do not recommend that this construct be commonly used, I do recommend that it be commonly known. It should be available to the expert C++ programmer for those rare occasions when its use is required or preferable to other constructs. It's part of the C++ language for a reason.

Surprisingly, even the predefined index operator is often misunderstood. We all know that both array names and pointers may be indexed:



int ary[12]; 


int *p = &ary[5];


p[2] = 7;


The predefined index operator is just a shorthand for some pointer arithmetic and a dereference. The expression p[2] above is entirely equivalent to *(p+2). Most C++ programmers with a C background are also aware that it's legal to use negative indexes, so the expression p[-2] is well defined and equivalent to *(p-2) or, if you prefer, *(p+-2). However, it doesn't seem to be common knowledge that addition is commutative, since most C++ programmers are surprised to find that it's legal to index an integer with a pointer:



(-2)[p] = 6; 


It's a simple transformation: p[-2] is equivalent to *(p+-2), which is equivalent to *(-2+p), which is equivalent to (-2)[p] (we need the parentheses because [] has higher precedence than unary minus).

What's the use of this bit of trivia? Well, for one thing, note that this commutativity of the index operator applies only to its predefined use with pointers. That is, if we see an expression like 6[p], we know we're dealing with the predefined index operator rather than with an overloaded member operator [] (though p is not necessarily a pointer or array). It's also terrific when conversation lags at cocktail parties. However, before employing this syntax in production code, review Gotcha #11.

Most C++ programmers know that a switch-statement is pretty basic. They just don't know how basic. The abstract syntax of the switch-statement is simple:



switch( expression ) statement 


The implications of this simple syntax are sometimes surprising.

Typically, the substatement that follows the switch expression is a block. Within the block is a set of case labels that implement basically a computed goto to a statement within the block. The first subtlety that new C and C++ programmers face is the concept of "fallthrough." That is, unlike many other modern programming languages, after a switch branches to the proper case label, its work is done. Where execution leads after that is totally up to the programmer:



switch( e ) { 


default:


theDefault:


   cout << "default" << endl;


   // fallthrough . . .


case 'a':


case 0:


   cout << "group 1" << endl;


   break;


case max-15:


case Select<(MAX>12),A,B>::Result::value:


   cout << "group 2" << endl;


   goto theDefault;


}


Conventionally, whenever fallthrough is used on purpose—as opposed to its more typical inadvertent use—we insert a comment to indicate to future maintainers that we actually intended the fallthrough. Otherwise, maintainers have a tendency to insert inappropriate breaks.

Note that the case labels must be integer constant-expressions. In other words, the compiler must be able to determine their values at compile time. However, as the somewhat flaky example above shows, there is quite a lot of leeway in how constant expressions may be defined. The case expression itself must be integral, or it may be an object with a conversion to an integral type. For example, e could be the name of a class object that declares a conversion operator to an integral type.

Note that the abstract syntax of the switch implies that it's even less structured than our example above implies. In particular, the case labels may appear anywhere within the switch-statement, and not necessarily at the same level:



switch( expr ) 


   default:


   if( cond1 ) {


       case 1: stmt1;


       case 2: stmt2;


   }


   else {


       if( cond2 )


           case 3:stmt2;


       else


           case 0: ;


   }


This may look a bit silly (it is, actually), but these more esoteric aspects of the base language can be useful on occasion. The above property of the switch, for instance, has been used to implement efficient internal iteration of a complex data structure for a C++ compiler:

gotcha07/iter.cpp



bool Postorder::next() { 


  switch( pc )


  case START:


  while( true )


     if( !lchild() ) {


        pc = LEAF;


        return true;


  case LEAF:


        while( true )


           if( sibling() )


              break;


           else


              if( parent() ) {


                 pc = INNER;


                 return true;


  case INNER:   ;


              }


              else {


                 pc = DONE;


  case DONE:    return false;


              }


     }


}


In the above code, we were able to use the esoteric semantics of the lowly switch-statement to implement coroutine semantics for the next tree traversal operation.

I've received strong, negative, and sometimes abusive reactions to my use of every one of the constructs above. I do agree this is not necessarily the kind of code you'd want to unleash on a novice maintainer, but such constructs—suitably encapsulated and with accompanying documentation—do have an occasional place in highly tuned or highly specialized code. Familiarity with the esoterica of the base language can be useful.

    [ Team LiB ] Previous Section Next Section