根据-Wstrict-overflow
3 级的文档:
还警告[s] 关于简化比较的其他情况。例如:x + 1 > 1 被简化为 x > 0。
下面显示的 MWE 在级别 3 及以上(但不低于)以及如果优化设置为及以上但不低于以下时会引发以下警告。-O2
g++ 版本 9.3.0 和 10.2 展示了这一点。
$ g++ -O3 -Wall -Wextra -pedantic -std=c++17 -Wstrict-overflow=3 a.cpp
a.cpp: 在函数 'void std::push_heap(_RAIter, _RAIter) [with _RAIter = long unsigned int *]':a.cpp:8:1:警告:假设将 X +- C1 cmp C2 更改为 X cmp C2 -+ C1 [-Wstrict-overflow] 时不会发生有符号溢出
MWE
#include <algorithm>
int main() {
std::size_t v[] = {0,10,3};
std::make_heap(std::begin(v),std::end(v));
std::pop_heap(std::begin(v),std::end(v));
std::push_heap(std::begin(v),std::end(v)); // <---
}
问题
- 这是库实现中的错误吗?我没有看到任何签名类型。
- 如何在保持
-Wstrict-overflow
最高 5 级的同时解决此问题?