On Sun, Nov 25, 2018 at 3:05 PM Gavin Lambert via Boost < boost@lists.boost.org> wrote:
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
(x); if (xu < f()) This expresses your intent of using the unsigned version of the parameter.
You probably mean
auto xu = std::make_unsigned
assert(x>=0);
That helps -- assuming that people actually check asserts, which isn't always the case.
Ironically, in my experience the warning police doesn't care about the assert, even though it will actually catch the bugs they're trying to defend against, while the warning, silenced with a cast, won't.