gpt4 book ai didi

c++ - boost::hash_combine 在 Release模式下失败

转载 作者:行者123 更新时间:2023-11-28 04:46:13 25 4
gpt4 key购买 nike

我在自定义哈希对象中使用 boost::hash_combine 为 std::array 定义二维网格中的位置。

struct PositionHasher {
std::size_t operator()(const std::array<int, 2> &position) const {
std::size_t seed;
boost::hash_combine(seed, position[0]);
boost::hash_combine(seed, position[1]);
return seed;
};
};

对 boost::hash combine 的调用等同于:

seed ^= position[0] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= position[1] + 0x9e3779b9 + (seed << 6) + (seed >> 2);

在 Release模式下构建我的应用程序时,我得到了与 Debug模式下不同的散列行为。我怀疑我实际上对相同的 std::array 对象有不同的值。即使我从函数中删除 0x9e3779b9,此行为仍然存在。

这怎么可能?我正在使用 VS2015 和完整的/Ox 优化,并且正在使用自定义哈希来查找 std::unordered_set 中的位置对象。

最佳答案

我发现了这个错误 - 一个愚蠢的错误,但也许这个解决方案对其他人有帮助:正如您在上面的代码中看到的,我没有为种子初始化 std::size_t 变量。在 Debug模式下,它默认初始化为 0。但是,这不会在优化下发生,导致不一致的行为/相同数据的不同哈希值。

有关 Release模式中容易发生的错误的更多信息:Common reasons for bugs in release version not present in debug mode

关于c++ - boost::hash_combine 在 Release模式下失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49213348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com