stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: Another style question
Date Wed, 16 Apr 2008 18:32:38 GMT
Eric Lemings wrote:
> I've noticed a lot of #ifdef and #ifndef directives in the code.

Yeah, about 3500 of them.

> Personally, I prefer to always use plain old #if directives since
> a compound `#if defined foo || defined bar` is more readable and
> consistent with simple #if directives IMHO than
> `#ifdef foo || defined bar`.

Only the first (verbose) form is valid. We have close to 900 of
those.

>  
> Just wondering if there is a preference here.  Would a simple
> `#if defined foo` be permissible?

We've been mostly using the short form for simple conditionals and
the defined operator only when necessary (i.e., in #elif directives
and in #if directives involving boolean expressions).

The rationale is that first, the Rogue Wave C++ Standard Library
style document specified that code that needed workarounds be
guarded by an #ifndef directive, with the correct code first
and the workaround in the #else branch. E.g.,

   #ifndef _RWSTD_NO_MEMBER_TEMPLATES
       template <class Iterator> void insert (Iterator, Iterator);
   #else
       // workaround for no member templates
       void insert (const_pointer, const_pointer);
   #endif

and second, because the macro names tend to be long and the short
form makes the code less verbose.

As with every other convention, we're not 100% consistent, only
about 90% by my count.

Btw., a more flexible way to code these workarounds would have been
to code it like so:

   #if !_RWSTD_NO_MEMBER_TEMPLATES
       // correct code
   #else
       // workaround
   #endif

This way, each config macro could be #defined on the command line
to 0 or 1 to toggle either branch (e.g., for testing and debugging).
That's not possible with the #ifndef approach.

Incidentally, speaking of #ifdefs, the other convention is to
add a comment after the #else and #endif directives mentioning
the name of the macro(s) used in the conditional (or some such
text) to help see which which branch is active. So according
to this convention the code above would look like so:

   #ifndef _RWSTD_NO_MEMBER_TEMPLATES
       // correct code
   #else   // ifdef (_RWSTD_NO_MEMBER_TEMPLATES)
       // workaround
   #endif   // _RWSTD_NO_MEMBER_TEMPLATES

Martin

Mime
View raw message