为什么 noexcept(true) 在 noexcept 工作的地方会失败?

Jan*_*tke 5 c++ clang language-lawyer compiler-bug noexcept

我有以下代码:

struct S {
    // conditional noexcept specification which depends on noexceptness of foo()
    void bar() noexcept(noexcept(foo()));
    // conditional noexcept specification (true)
    void foo() noexcept(true);
};
Run Code Online (Sandbox Code Playgroud)

Clang 拒绝此代码,但 GCC 允许它(https://godbolt.org/z/nxqa5Tr1x):

<source>:2:34: error: exception specification is not available until end of class definition
    2 |     void bar() noexcept(noexcept(foo()));
      | 
Run Code Online (Sandbox Code Playgroud)

替换noexcept(true)noexcept两个编译器都允许

问题

  1. 为什么会noexcept(true)失败但是noexcept成功?
  2. 哪种编译器符合该标准(如果有)?

注意:这个例子显然是最小的。我的实际用例是,我有一个clear()成员函数,仅在自定义容器中为noexceptif clear_impl()is 。noexcept

Bri*_*ian 4

  1. 没有理由noexcept并且noexcept(true)应该有不同的行为。
  2. 此问题似乎与 CWG 问题 1360、1397、1890 和 2335 相关。noexcept 说明符是完整类上下文。这意味着在noexcept-specifier中,S是完整的(例如,您可以看到 的成员的完整列表S),但并不意味着在noexcept-specifier中,您可以访问稍后的完整类上下文中的信息。在某些完整类上下文中,某些编译器甚至不允许您访问不同类型的早期完整类上下文中的信息,并且措辞不够清晰,无法确定这是一个编译器错误。我在这里更详细地解释。