Boost logo

Boost :

Subject: Re: [boost] [mpl] clang error for narrowing from unsigned int 0xffffffff to long
From: Edward Diener (eldiener_at_[hidden])
Date: 2013-11-13 23:22:30


On 11/13/2013 5:37 PM, Jonathan Wakely wrote:
> 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

Or my own preference of 'static_cast<long>(0xffffffff)'. But using
'0xffffffffL' does not work. I admit that understanding the C++
Standard's different interprtetation of these two forms is beyond me.


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