g++ 和严格溢出的问题
problems with g++ and strict-overflow
我返回到一个旧项目,发现它不再使用最新的 g++ 版本 (5.2.0) 进行编译。
我收到神秘错误:
src/combos.cpp: In member function ‘void ComboHandler::execute(uint64_t) const’
src/combos.cpp:123:6: error: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Werror=strict-overflow]
void ComboHandler::execute(const uint64_t Mods) const {
^
src/combos.cpp:123:6: error: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Werror=strict-overflow]
src/combos.cpp:123:6: error: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Werror=strict-overflow]
通过注释掉该函数中的代码块,直到它再次编译,我将错误追溯到这一行:
tmpCont.insert(actionPair(el,tmpParams));
其中 tmpCont
的类型是 std::set<actionPair, execOrder>
,其中execOrder
是:
struct execOrder {
bool operator() (const actionPair& i, const actionPair& j) const {
/* keep comboObjects with identical combos */
if((i.first->Keys==j.first->Keys) && (i.first->Mods==j.first->Mods)) return true;
/* comboObjects match if at least one key matches */
for(const Combo::key_type::value_type &elval: i.first->Keys)
if(std::find(j.first->Keys.begin(),j.first->Keys.end(),elval)!=j.first->Keys.end()) {
/* don't keep matching combos */
return false;
}
return true;
}
和Keys
是std::vector<uint64_t>
。如果我替换第二个 if
块中的 std::find(...)
语句,g++ 会成功编译此代码。但是,我还是一头雾水,不知道怎么解决这个问题。
我的编译器标志是
-O2 -Wall -Wextra -std=c++11 -pedantic `sdl2-config --cflags` -Wabi -fabi-version=0 -ffor-scope -fstrict-enums -fuse-cxa-atexit -Wctor-dtor-privacy -Wnoexcept -Wstrict-null-sentinel -Wold-style-cast -Woverloaded-virtual -Wsign-promo -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dirs -Wswitch-default -Wswitch-enum -Wunused-local-typedefs -Wuninitialized -fstrict-overflow -Wstrict-overflow=5 -Wtrampolines -Wfloat-equal -Wundef -Wshadow -Wcast-qual -Wcast-align -Wconversion -Wsign-conversion -Wlogical-op -Wmissing-declarations -Wpacked -Wredundant-decls -Winline -Wvector-operation-performance -Wno-unknown-pragmas -Wdeprecated -Wno-inline -Wno-switch-default -DDEBUG -g -Werror -pedantic-errors
(当时基本上是 clang 的 -Weverything)
无论如何,这是一个 GCC 错误。 std::find
代码正确但警告错误,或者警告正确但 std::find
实现无法正确处理所有边缘情况。这要由 GCC 维护者来解决。
作为解决方法,turn off the warning 仅针对那几行。
我返回到一个旧项目,发现它不再使用最新的 g++ 版本 (5.2.0) 进行编译。
我收到神秘错误:
src/combos.cpp: In member function ‘void ComboHandler::execute(uint64_t) const’
src/combos.cpp:123:6: error: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Werror=strict-overflow]
void ComboHandler::execute(const uint64_t Mods) const {
^
src/combos.cpp:123:6: error: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Werror=strict-overflow]
src/combos.cpp:123:6: error: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Werror=strict-overflow]
通过注释掉该函数中的代码块,直到它再次编译,我将错误追溯到这一行:
tmpCont.insert(actionPair(el,tmpParams));
其中 tmpCont
的类型是 std::set<actionPair, execOrder>
,其中execOrder
是:
struct execOrder {
bool operator() (const actionPair& i, const actionPair& j) const {
/* keep comboObjects with identical combos */
if((i.first->Keys==j.first->Keys) && (i.first->Mods==j.first->Mods)) return true;
/* comboObjects match if at least one key matches */
for(const Combo::key_type::value_type &elval: i.first->Keys)
if(std::find(j.first->Keys.begin(),j.first->Keys.end(),elval)!=j.first->Keys.end()) {
/* don't keep matching combos */
return false;
}
return true;
}
和Keys
是std::vector<uint64_t>
。如果我替换第二个 if
块中的 std::find(...)
语句,g++ 会成功编译此代码。但是,我还是一头雾水,不知道怎么解决这个问题。
我的编译器标志是
-O2 -Wall -Wextra -std=c++11 -pedantic `sdl2-config --cflags` -Wabi -fabi-version=0 -ffor-scope -fstrict-enums -fuse-cxa-atexit -Wctor-dtor-privacy -Wnoexcept -Wstrict-null-sentinel -Wold-style-cast -Woverloaded-virtual -Wsign-promo -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dirs -Wswitch-default -Wswitch-enum -Wunused-local-typedefs -Wuninitialized -fstrict-overflow -Wstrict-overflow=5 -Wtrampolines -Wfloat-equal -Wundef -Wshadow -Wcast-qual -Wcast-align -Wconversion -Wsign-conversion -Wlogical-op -Wmissing-declarations -Wpacked -Wredundant-decls -Winline -Wvector-operation-performance -Wno-unknown-pragmas -Wdeprecated -Wno-inline -Wno-switch-default -DDEBUG -g -Werror -pedantic-errors
(当时基本上是 clang 的 -Weverything)
无论如何,这是一个 GCC 错误。 std::find
代码正确但警告错误,或者警告正确但 std::find
实现无法正确处理所有边缘情况。这要由 GCC 维护者来解决。
作为解决方法,turn off the warning 仅针对那几行。