在映射中使用结构作为键,忽略 == 运算符,误报
Using Structs as keys in maps, ignoring == operators, false positive
我正在尝试使用结构作为映射中的键,但尽管定义了<
和==
运算符,我还是得到了误报:
即使新ImportSettings
reverse = true
并且模型中的具有"反向=假",return models[mr];
也会返回。
Visual Studio 调试器还显示,甚至没有调用运算符对==
的调用。我在这里误解了什么?
struct HE2_ImportSettings
{
bool reverseWindingOrder = false;
bool operator==(const HE2_ImportSettings& other)const
{
return reverseWindingOrder == other.reverseWindingOrder;
}
};
struct ModelReference
{
std::string filepath = "";
HE2_ImportSettings importSettings = {};
bool ModelReference::operator == (const ModelReference& other) const
{
return filepath == other.filepath && importSettings == other.importSettings;
}
bool ModelReference::operator< (const ModelReference& other) const
{
return filepath < other.filepath;
}
};
ModelReference mr = { filename, importSettings };
bool exists = models.count(mr);
if (exists)
{
//This is returning even when the new importsettings has reverse = true and the one in models has reverse = false
return models[mr];
}
>std::map
使用等价的概念。两个元素在以下情况下是等效的
!(a < b) && !(b < a)
换句话说,地图从不使用==
来查看两个元素是否相同。在地图中,当两个元素具有相同的filepath
时,它们被视为等效。如果您还想importSettings
考虑地图中的排序,则需要在operator<
中进行比较。
相关文章:
- 并行块(线程清理器)之外的 OpenMP 中的争用条件;误报?
- 在映射中使用结构作为键,忽略 == 运算符,误报
- Clang 分析器潜在内存泄漏 - 误报
- 未定义的行为或误报
- C++ UBSAN 对派生对象产生误报
- 解释瓦尔格林德输出(可能的误报?
- 从特征匹配/单应性中过滤掉误报 – OpenCV
- Bloom过滤器误报
- 通过shared_ptr同步:线程清理器误报
- 多边形汤 - elipsoid碰撞误报(Kasper fauerby)
- 误报警告PVS Studio:V808类型的对象是创建但未使用的
- 误报警告 PVS 工作室:V821 性能降低。'rhs'变量可以在较低级别的范围内构造
- 禁用 Eclipse 的错误发现。(科丹误报)
- 如何避免 CFile查找文件扩展名超过三个字符的误报
- 用户泄漏、libc++泄漏或误报
- ThreadSanitizer说我的Atomic Inc / Dec有数据竞争,误报
- 在自定义异常中仍可访问(误报?
- Eclipse 索引器显示误报语义错误
- 使用clang的ThreadSanitizer和TBB避免误报
- 向量<unique_ptr>上is_copy_constructible的误报