为什么我无法在win64上编译带有boost 1.54的VS2012 C++代码

Why I fail to compile VS2012 C++ code with boost 1.54 over win64?

本文关键字:boost VS2012 代码 C++ win64 编译 为什么      更新时间:2023-10-16

我有一个使用VS2010和boost 1_54_0版本编译的win64应用程序,一切如预期。

我现在正在将应用程序转移到一个新的平台,该平台需要VS2012编译的库。当试图用VS2012编译boost时(稍后链接到我的项目中),我得到了以下编译器警告:

 1>c:<my_path>boostboost_1_54_0boostfunctionalhashhash.hpp(176): warning C6295: Ill-defined for-loop:  'unsigned int' values are always of range '0' to '4294967295'.  Loop executes infinitely.
 1>C:<my_path>boostboost_1_54_0boost/functional/hash/hash.hpp(201) : see reference to function template instantiation 'size_t boost::hash_detail::hash_value_unsigned<T>(T)' being compiled
 1>          with
 1>          [
 1>              T=boost::ulong_long_type
 1>          ]
 1>          C:<my_path>boostboost_1_54_0boost/functional/hash/hash.hpp(439) : see reference to function template instantiation 'boost::hash_detail::enable_hash_value::type boost::hash_value<boost::ulong_long_type>(T)' being compiled
 1>          with
 1>          [
 1>              T=boost::ulong_long_type
 1>          ]

<my_path>表示我的开发机器上的本地路径,所以这可以忽略)

环顾hash.hpp文件的#176行(例如),我看到了以下

    template <class T>
    inline std::size_t hash_value_unsigned(T val)
    {
         const int size_t_bits = std::numeric_limits<std::size_t>::digits;
         // ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
         const int length = (std::numeric_limits<T>::digits - 1)
             / size_t_bits;
         std::size_t seed = 0;
         // Hopefully, this loop can be unrolled.
         for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
         {
             seed ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2);
         }
         seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
         return seed;
    }

第#176行是for语句:for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)

现在我似乎不明白编译器到底在警告我什么?如果条件是i>=0,这是有意义的(根据MSDN对C6295的解释),但for语句逻辑对我来说还可以

此警告的根本原因是什么?如何解决它?

另外,由于我的应用程序使用了警告级别4(警告被视为错误)-由于这个警告,我无法编译应用程序。

感谢

这是在Boost中修复的:https://svn.boost.org/trac/boost/ticket/8568.

我建议更新到Boost 1.55,在本地修补Boost副本,或者使用/wd6295或Boost includes周围的pragma禁用该特定警告。

尽管在这种情况下可能不适用于您,但这就是为什么在已发布的源代码中包含的构建脚本中强制警告=错误通常是一件坏事:新的编译器版本会添加新的警告。