Boost logo

Boost :

Subject: Re: [boost] [units] gcc warnings
From: Emil Dotchevski (emil_at_[hidden])
Date: 2010-06-17 02:28:24

On Wed, Jun 16, 2010 at 10:16 AM, Stewart, Robert
<Robert.Stewart_at_[hidden]> wrote:
> I don't see how static_cast would be able to hide an error.

The general point is that casts do more than simply silence warnings. Example:

void foo( short x );
int value();
foo(static_cast<short>(value())); //static_cast used to suppresses a warning

Later under maintenance we introduce an additional foo overload:

void foo( long x );

Now the call to foo with the static_cast<short> will continue to
compile while without the static_cast you'd get a compile error.

> I suggested an assertion in addition to the static_cast to verify the behavior as
> a defense against maintenance effects. Assuming the type is always short,
> then asserting the current value less-than std::numeric_limits<short>::max()
> would suffice. To account for your change of type scenario, a compile-time
> assertion that the type is short would also be needed.

I'll illustrate with code again:

void foo( short a, short b )
  short result=a+b;

In general, I find it difficult to accept this instead:

void foo( short a, short b )
  int tmp=a+b;
  short result=static_cast<short>(tmp);

In some specific case -- maybe, but I can't justify asserts every time
I deal with shorts (which admittedly isn't very often.)

Emil Dotchevski
Reverge Studios, Inc.

Boost list run by bdawes at, gregod at, cpdaniel at, john at