Brent Dax <[EMAIL PROTECTED]> writes:

> What about little inline things?

> AUTO_OP sleep(i|ic) {
>       #ifdef WIN32
>               Sleep($1*1000);
>       #else
>               sleep($1);
>       #endif
> }

This reminds me.  gcc is slowly switching over to writing code like that
as:

    if (WIN32) {
        Sleep($1*1000);
    } else {
        sleep($1);
    }

or the equivalent thereof instead of using #ifdef.  If you make sure that
the values are defined to be 0 or 1 rather than just defined or not
defined, it's possible to write code like that instead.

This has the significant advantage that the compiler will continue to
syntax-check the code that isn't active on the build platform, making it
much less likely that one will get syntax errors in the code not active on
the platform of the person doing the patching.  The dead-code-elimination
optimization phase of any decent compiler should dump the dead paths
entirely.

It may not be possible to use this in cases where the not-taken branch may
refer to functions that won't be prototyped on all platforms, depending on
the compiler, but there are at least some places where this technique can
be used, and it's worth watching out for.

(In the case above, I'd probably instead define a sleep function on WIN32
that calls Sleep so that the platform differences are in a separate file,
but there are other examples of things like this that are better suited to
other techniques.)

-- 
Russ Allbery ([EMAIL PROTECTED])             <http://www.eyrie.org/~eagle/>

Reply via email to