Boost logo

Boost :

From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2006-06-29 15:54:49


Paul A Bristow wrote:
> IMO Boost-worthy code should be able to be compiled in strict mode, for MSVC
> level 4, without producing a blizzard of warnings.
>
> Including a list of warnings that the authors of the library consider
> unhelpful, and in some cases, unfixable (specifically the warning
> "conditional expression is constant. ")
>
> by using, for example
>
> #ifdef _MSC_VER
> # pragma warning(disable: 4127) // conditional expression is constant.
> #endif

This warning is not so bad, IMO. It tells you that you are trying
evaluate a compile-time constant in run-time, and that's wasting CPU
cycles. The runtime 'if' can be substituted by a compile-time
specialization. E.g, instead of

     if (compile-time-bool-constant)
     {
         ...
     }
     else
     {
         ...
     }

you can write:

     template <bool Cond>
     void foo(...);

     template <>
     void foo<false>(...) { ... }

     template <>
     void foo<true>(...) { ... }

     ...

     foo<compile-time-bool-constant>(...);

or something similar.

This will prevent the compiler from generating the runtime comparison.
Sure this is a small piece of code we are trying to optimize, and a good
optimizing compiler would remove it anyway, but if we can write more
efficient code without relying on a compiler optimization that might or
might not be there, isn't it better?

Yuval

P.S. if someone really insists on suppressing this warning, but doesn't
like my suggestion, he could probably write (untested if this really works)

     bool b = compile-time-bool-constant;
     if (b) { ... } else { ... }

although I really don't like it...


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk