Boost logo

Boost :

Subject: Re: [boost] Current Guidance on Compiler Warnings?
From: Rob Stewart (rstewart_at_[hidden])
Date: 2018-11-25 01:19:28


On November 23, 2018 2:58:44 PM EST, Emil Dotchevski via Boost <boost_at_[hidden]> wrote:
>
> I'm guessing you want an example where the explicit conversion is
> incorrect, and you want to use an implicit conversion even though you
> get a warning. Here:
>
> unsigned f();
>
> void g( int x )
> {
> if( x < f() ) //warning C4018: '<': signed/unsigned mismatch
> {
> ....
> }
> }
>
> So you change that to:
>
> if( static_cast<unsigned>(x) < f() )

Why is that the appropriate cast? If your argument is that such a cast is the likely change applied by an ignorant maintainer trying to silence a warning, then that's not a good argument. An ignorant maintainer can make all sorts of breaking changes.

> Then under refactoring both f and g get changed:
>
> unsigned long f();
>
> void g( long x )
> {
> if( static_cast<unsigned>(x) < f() )
> {
> ....
> }
> }
>
> And now you probably have a bug, and no warning.

Your point that a cast may not be the best solution to eliminating warnings is valid. Assertions, range checking, or library solutions are necessary to know that operations are safe. With those in place, pragmas and casts can be used to silence warnings.

You may argue that the implicit conversion is correct, but it is as much subject to breakage by future maintenance as your example.

--
Rob
(Sent from my portable computation device.)

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