Boost logo

Boost :

Subject: Re: [boost] [mpl] clang error for narrowing from unsigned int 0xffffffff to long
From: Jonathan Wakely (jwakely.boost_at_[hidden])
Date: 2013-11-13 17:37:42


On 13 November 2013 20:46, Edward Diener wrote:
> In the mpl bitwise.cpp test there are lines like:
>
> MPL_ASSERT_RELATION( (bitor_<_0,_ffffffff>::value), ==, 0xffffffff );
> MPL_ASSERT_RELATION( (bitor_<_1,_ffffffff>::value), ==, 0xffffffff );
> MPL_ASSERT_RELATION( (bitor_<_8,_ffffffff>::value), ==, 0xffffffff );
>
> Using clang under Windows with the VC++ RTL ( clang-cl ), clang gives an
> error of:
>
> bitwise.cpp(43,25) : error: non-type template argument evaluates to
> 4294967295, which cannot be narrowed to type 'long' [-Wc++11-narrowing]
> MPL_ASSERT_RELATION( (bitor_<_0,_ffffffff>::value), ==, 0xffffffff );
>
> for the bitor_<_0,_ffffffff>::value expression part since the
> assert_relation template takes two 'long's as its values.
>
> Is this an actual bug according to the C++11 standard ?

Yes.

> I would have thought
> that an unsigned int value of 0xffffffff would fit into a long as a -1,

It's not a matter of fitting into a given number of bytes, the value
would not be the same, so it's not a value-preserving conversion.

> but
> it sounds like the new standard is saying that since the value of 4294967295
> cannot be encompassed by a long it is an error.

Yes, in certain contexts where narrowing conversions are forbidden,
notably as non-type template parameters or inside braced
initialization lists.

> if it is an error how should it be fixed in MPL ?

You can use an explicit conversion, e.g. (long)0xffffffff


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