我有一个使用 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> ]
(<my_path>
代表我开发机器上的本地路径,所以这个可以忽略)
在 # 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 > 0; i -= size_t_bits)
.
现在我似乎不明白编译器到底警告我什么?如果条件是 i>=0
这是有道理的(根据 C6295 的 MSDN 解释)但是 for
语句逻辑在我看来没问题。
此警告的根本原因是什么?如何解决?
附言因为我的应用程序使用警告级别 4(警告被视为错误)- 由于此警告,我无法编译我的应用程序。
谢谢
最佳答案
这已在 Boost 中修复:https://svn.boost.org/trac/boost/ticket/8568 .
我建议更新到 Boost 1.55,在本地修补您的 Boost 拷贝,或者使用 /wd6295
或 Boost 周围的 pragma includes 禁用该特定警告。
虽然在这种情况下可能不适用于您,但这就是在已发布源代码中包含的构建脚本中强制使用 warnings=errors 通常是一件坏事的原因:新的编译器版本会添加新的警告。
关于c++ - 为什么我无法在 win64 上使用 boost 1.54 编译 VS2012 C++ 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23245835/