|
Boost : |
Subject: Re: [boost] Current Guidance on Compiler Warnings?
From: Gavin Lambert (boost_at_[hidden])
Date: 2018-11-25 23:04:45
On 24/11/2018 08:58, Emil Dotchevski 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() )
>
> Then under refactoring both f and g get changed:
>
> unsigned long f();
>
> void g( long x )
> {
> if( static_cast<unsigned>(x) < f() )
> {
> ....
> }
> }
auto xu = std::make_unsigned_t<decltype(x)>(x);
if (xu < f())
This expresses your intent of using the unsigned version of the parameter.
Although in reality you should probably just change the type of the
parameter, because both of the above are wrong unless you're
constraining it to not accept negative values anyway.
> assert(x>=0);
That helps -- assuming that people actually check asserts, which isn't
always the case.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk