Boost logo

Boost Users :

Subject: Re: [Boost-users] [numeric_cast] Warning with MSVC
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-07-16 12:18:26


On Thu, Jul 16, 2009 at 8:29 AM, Andrew
Holden<aholden_at_[hidden]> wrote:
> Alexander Lamaison wrote:
> Sent: Thursday, July 16, 2009 6:09 AM
>> I'm using boost::numeric_cast to convert the size of a
>> wstring (presumably a size_t) to an int.
>>
>>  const int size = boost::numeric_cast<int>(my_string.size());
>>
>> MSVC still gives me a warning about possible loss of data:
>>
>>  c:\program files\boost\boost_1_38\boost\numeric\conversion\
>>  cast.hpp(41) : warning C4267: 'argument' : conversion from 'size_t'
>>  to 'unsigned int', possible loss of data
>>  c:\users\awl03\documents\visual studio 2005\projects\swish_trunk
>>  (padd)\swish\utils.hpp(91) : see reference to function template
>>  instantiation 'Target boost::numeric_cast<int,__w64 unsigned
>>  int>(Source)' being compiled
>>        with
>>        [
>>            Target=int,
>>           Source=__w64 unsigned int
>>        ]
>>  c:\users\awl03\documents\visual studio 2005\projects\swish_trunk
>>  (padd)\swish\utils.hpp(121) : see reference to function template
>>  instantiation 'std::basic_string<_Elem,_Traits,_Ax>
>>  swish::utils::ConvertString<`anonymous-namespace'::Narrow>(const
>>  std::basic_string<wchar_t,std::char_traits<wchar_t>,std::
>>  allocator<_Ty>> &)' being compiled
>>        with
>>        [
>>            _Elem=char,
>>            _Traits=std::char_traits<char>,
>>            _Ax=std::allocator<char>,
>>            _Ty=wchar_t
>>        ]
>>
>> Unless I misunderstand, isn't the idea of numeric_cast that
>> warnings aren't needed because the casts are checked for loss
>> of data?  Any advice is greatly appreaciated.
>>
>> I am using the MSVC 8.0 compiler with Boost 1.38.0 on 32-bit Windows
> Vista.
>>
>> Thanks.
>>
>> Alex Lamaison
>
> The issue is a compiler option called "detect 64-bit portability issues"
> or "/Wp64".  As it is both non-standard and a rather brutal hack, there
> is probably no way for Boost to support it.
>
> I would recommend turning that option off.

For what it's worth, this compiler option is deprecated in recent
versions of MSVC, so that adds one more to the list of reasons to
disable the option.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net