【问题标题】:"err:seh:setup_exception stack overflow" when compiling constexpr code in MSVC在 MSVC 中编译 constexpr 代码时出现“err:seh:setup_exception 堆栈溢出”
【发布时间】:2017-10-18 16:34:35
【问题描述】:

编译以下代码(godbolt):

constexpr bool f(const char *&s) {
  do {
    ++s;
  } while (*s);
  return true;
}

constexpr bool g(const char *s) {
  return f(s);
}

int main() {
  static_assert(g("x"), "");
}

在 MSVC 上出现此错误:

err:seh:setup_exception stack overflow 1552 bytes in thread 0058 eip 000000007b48dad8 esp 0000000000131000 stack 0x130000-0x131000-0x1130000

而其他编译器(GCC 和 Clang)很乐意接受它。

为什么这段代码不能在 MSVC 上编译,我该如何解决?

【问题讨论】:

  • 我能想到的唯一一件事是您在 进行空检查之前增加了指针。这将在空字符串上调用未定义的行为。我不知道为什么这里有问题。在 Godbolt 中更改代码以在递增之前进行检查似乎也不能解决问题。
  • 很明显是一个错误。即使该代码无效,这也不是 MSVC 应该给您的消息。

标签: c++ constexpr


【解决方案1】:

我能够通过从函数签名 (Godbolt) 中删除引用限定符来编译它:

constexpr bool f(const char *s) {//No longer passes a pointer by reference
  while(*s) { //This avoids undefined behavior when the passed string is empty
    ++s;
  }
  return true;
}

constexpr bool g(const char *s) {
  return f(s);
}

int main() {
  static_assert(g("x"), "");
}

我不知道为什么通过引用传递指针会导致这个错误,我坚持认为这可能是 MSVC 编译器中的一个错误。您应该将错误发布到他们的bug tracking forum

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2011-05-25
    • 1970-01-01
    相关资源
    最近更新 更多