|
Boost : |
From: Björn Karlsson (Bjorn.Karlsson_at_[hidden])
Date: 2002-09-28 14:30:57
> From: Craig Henderson [mailto:cdm.henderson_at_[hidden]]
>
> I personally don't like using static_cast in such a way - it
> clutters the
> code and suggests a hack, IMO. Anyway, I tried the following three
> alternatives which I would have expected to work, but none of
> them did.
Eh, but how could this be a hack? There's nothing wrong with the code in the
first place - this is a compiler problem. Furthermore, using static_cast as
a way of expressing "I know what I'm doing here" is often considered good
practice.
> return static_cast<unsigned char>(x ^ ( rem >> (DoReflect ?
> 0u : Bits -
> CHAR_BIT) ) );
>
> return x ^ static_cast<unsigned char>( ( rem >> (DoReflect ?
> 0u : Bits -
> CHAR_BIT) ) );
>
> return static_cast<unsigned char>( x ^ static_cast<unsigned
> char>( ( rem >>
> (DoReflect ? 0u : Bits - CHAR_BIT) ) ) );
No, none of these work, because of the faulty runtime check. It does,
however, suppress the compiler warning (C4244 for VC.NET).
> Is my original fix of this (modified slightly in response to
> Dirk Gerrits)
> really that much of a performance hit? I don't know without
> running some
> tests.
> return UCHAR_MAX & (x ^ ( rem >> (DoReflect ? 0u : Bits -
> CHAR_BIT) ) );
I don't know either, but I'd like to stress the fact that there's nothing
wrong with the existing code. A change should probably be made for a
specific compiler (VC.NET), with a specific flag (/RTCc) set. It's only then
that the workaround is necessary. That is, if anything should be changed at
all.
Finally, and it's none of my business - but I've got to ask: Why would you
want to defer this check to runtime, as it's always known at compile time? I
was baffled to see that this compiler option even existed for a statically
typed language.
[Tip: For code dealing with (tricky) integral conversions, consider using
numeric_cast.]
Bjorn Karlsson
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk