简体   繁体   English

g++ 和严格溢出的问题

[英]problems with g++ and strict-overflow

I returned to an old project to find that it no longer compiles with the latest g++ version (5.2.0).我回到一个旧项目,发现它不再使用最新的 g++ 版本 (5.2.0) 进行编译。
I get the cryptic error:我得到了一个神秘的错误:

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]

By commenting out blocks of code in that function until it compiled again, I traced the error to this line:通过注释该函数中的代码块直到它再次编译,我将错误追溯到这一行:

            tmpCont.insert(actionPair(el,tmpParams));

where tmpCont is of type std::set<actionPair, execOrder> , where execOrder is:其中tmpContstd::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;
}

and Keys are std::vector<uint64_t> .Keysstd::vector<uint64_t> If I replace the std::find(...) statement in the second if block, g++ successfully compiles this code.如果我替换第二个if块中的std::find(...)语句,g++ 将成功编译此代码。 However, I am still confused, and don't know how to fix this issue.但是,我仍然很困惑,不知道如何解决这个问题。

My compiler flags are我的编译器标志是

-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 -O2 -Wall -Wextra -std=c++11 -pedantic `sdl2-config --cflags` -Wabi -fabi-version=0 -ffor-scope -fstrict-enums -fuse-cxa-atexit -Wctor-dtor-隐私 -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

(essentially clang's -Weverything at the time) (基本上是叮当声 - 当时的一切)

One way or another, this is a GCC bug.无论如何,这是一个 GCC 错误。 Either the std::find code is correct and the warning is wrong, or the warning is right and the std::find implementation fails to properly handle all edge cases.要么std::find代码正确而警告错误,要么警告正确且std::find实现无法正确处理所有边缘情况。 That is up to the GCC maintainers to sort out.这由 GCC 维护者来解决。

As a workaround, turn off the warning for those few lines only.作为一种解决方法,仅关闭这几行的警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM