【发布时间】: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> ]
(<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,这是有道理的(根据 MSDN 对 C6295 的解释),但 for 语句逻辑对我来说看起来没问题。
此警告的根本原因是什么?如何解决?
附:因为我的应用程序使用了 4 级警告(警告被视为错误) - 由于此警告,我无法编译我的应用程序。
谢谢
【问题讨论】:
-
在这种情况下,长度可能为 0。 boost::ulong_long_type 和 std::size_t 是什么类型?
-
代码分析警告本质上是古怪的。我猜想减量不是 1 是不高兴的。因此,如果起始值不能被减量整除,那么从技术上讲,永远循环是可能的。当然可以,只要乘法不溢出。只需禁用警告。
标签: c++ c visual-studio-2012 boost 64-bit