【问题标题】:Why I fail to compile VS2012 C++ code with boost 1.54 over win64?为什么我无法在 win64 上使用 boost 1.54 编译 VS2012 C++ 代码?
【发布时间】:2014-04-23 13:27:56
【问题描述】:

我有一个使用 VS2010 和 boost 1_54_0 版本编译的 win64 应用程序 - 一切正常。

我现在将应用程序转移到需要 VS2012 编译库的新平台。 尝试使用 VS2012 编译 boost(以便稍后链接到我的项目)时,我收到以下编译器警告:

 1>c:\<my_path>\boost\boost_1_54_0\boost\functional\hash\hash.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>\boost\boost_1_54_0\boost/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>\boost\boost_1_54_0\boost/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>          ]

(&lt;my_path&gt;代表我的开发机器上的本地路径,可以忽略)

查看#176(例如)行的 hash.hpp 文件 - 我看到以下内容

    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 &gt; 0; i -= size_t_bits)。

现在我似乎不明白编译器到底警告我什么?如果条件是i&gt;=0,这是有道理的(根据 MSDN 对 C6295 的解释),但 for 语句逻辑对我来说看起来没问题。

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

附:因为我的应用程序使用了 4 级警告(警告被视为错误) - 由于此警告,我无法编译我的应用程序。

谢谢

【问题讨论】:

  • 在这种情况下,长度可能为 0。 boost::ulong_long_type 和 std::size_t 是什么类型?
  • 代码分析警告本质上是古怪的。我猜想减量不是 1 是不高兴的。因此,如果起始值不能被减量整除,那么从技术上讲,永远循环是可能的。当然可以,只要乘法不溢出。只需禁用警告。

标签: c++ c visual-studio-2012 boost 64-bit


【解决方案1】:

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

我建议更新到 Boost 1.55,修补您的 Boost localy 副本,或者使用 /wd6295 或 Boost 周围的编译指示禁用该特定警告。

虽然在这种情况下可能不适用于您,但这是在已发布源代码中包含的构建脚本中强制使用 warnings=errors 通常是一件坏事的原因:新的编译器版本会添加新的警告。

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多