Why would noexcept(true) fail where noexcept works?

131 views Asked by At

I have the following code:

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

Clang rejects this code but GCC allows it (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()));
      | 

Replacing noexcept(true) with noexcept is allowed by both compilers.

Questions

  1. Why would noexcept(true) fail but noexcept work?
  2. Which compiler conforms with the standard, if any?

Note: this example is obviously minimal. My actual use case is that I have a clear() member function which is noexcept only if clear_impl() is noexcept, in a custom container.

1

There are 1 answers

0
Brian Bi On
  1. There is no reason why noexcept and noexcept(true) should have different behaviour.
  2. This issue appears to be related to CWG issues 1360, 1397, 1890, and 2335. A noexcept-specifier is a complete-class context. This implies that within a noexcept-specifier, S is complete (e.g. you can see the full list of members of S), but it does not imply that within a noexcept-specifier, you have access to information that is in later complete-class contexts. In some complete-class contexts, some compilers don't even let you access information that is in earlier complete-class contexts of a different kind, and the wording is not clear enough to say for sure that this is a compiler bug. I explain in more detail here.