Boost logo

Boost :

Subject: Re: [boost] Official warnings policy?
From: John Maddock (john_at_[hidden])
Date: 2009-11-12 08:03:31


>> I'd be careful about adding a static_cast, since it changes exactly
>> what's being tested. Part of the behavior of a class is the way it
>> handles
>> implicit conversions. Even conversions that generate warnings sometimes
>> need to be tested.
>
> You are right, as ever. But in this case, is it my ignorance of C++ or is
> there
> no way of specifying an (unsigned) short,

There is no portable method of specifying an unsigned short literal, except
possible via the C99 macro UINT16_C in stdint.h.

> The test wanted to use v6 = 58US;
>
> Or unsigned short int i = 58U; v6 = i; ?
>
> Or would just v6 = 58U avoid the warning?
>
> In the test, I suspect that v6 = static_cast< t_var6 >(58); was what was
> really
> wanted (or some way of getting at the first template type from the variant
> that
> is beyond my skills).

Delving into this a bit deeper, I believe the warning is correct, and cannot
/ should not be fixed, to recap we have:

d:\data\boost\trunk\boost/variant/variant.hpp(1297) : warning C4244:
'argument' : conversion from 'const int' to 'const short', possible loss of
data
        d:\data\boost\trunk\boost/variant/variant.hpp(1366) : see reference
to function template instantiation 'void
boost::variant<T0_,T1>::convert_construct<const T>(T
&,int,boost::mpl::false_)' being compiled
        with
        [
            T0_=short,
            T1=const char *,
            T=int
        ]
        d:\data\boost\trunk\boost/variant/variant.hpp(1609) : see reference
to function template instantiation
'boost::variant<T0_,T1>::variant<int>(const T &)' being compiled
        with
        [
            T0_=short,
            T1=const char *,
            T=int
        ]
        d:\data\boost\trunk\boost/variant/variant.hpp(1619) : see reference
to function template instantiation 'void
boost::variant<T0_,T1>::assign<T>(const T &)' being compiled
        with
        [
            T0_=short,
            T1=const char *,
            T=int
        ]
        test1.cpp(66) : see reference to function template instantiation
'boost::variant<T0_,T1> &boost::variant<T0_,T1>::operator =<int>(const T &)'
being compiled
        with
        [
            T0_=short,
            T1=const char *,
            T=int
        ]

The code calls Variant's converting-assignment operator, and Boost.Variant
internally decides what type to convert the argument to and makes the
assignment. In this case the user *should* see a warning to be alerted to
the fact that the internal conversion results in loss of precision (likewise
they would see an error in this location if there is no suitable
conversion).

I notice that the code in question has a comment to this effect:

            // NOTE TO USER :
            // Compile error here indicates one of the source variant's
types
            // cannot be unambiguously converted to the destination
variant's
            // types (or that no conversion exists).

But perhaps we should add a similar note about compiler warnings?

John.


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