g ++和严格溢出的问题

时间:2015-09-09 12:08:02

标签: c++ c++11 g++

我回到了一个旧项目,发现它不再编译最新的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;
}

Keysstd::vector<uint64_t>。如果我替换第二个std::find(...)块中的if语句,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 -Wmpmpolines -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&#39; s-当时的一切)

1 个答案:

答案 0 :(得分:1)

这种或那种方式,这是一个GCC错误。 std::find代码是正确的,警告是错误的,或者警告是正确的,std::find实现无法正确处理所有边缘情况。这取决于GCC维护人员的整理。

作为解决方法,turn off the warning仅适用于这几行。