Skip to content

CWG3059 [expr.const]: throw; in constant expression #756

Description

@leni536

Full name of submitter: Lénárd Szolnoki

Reference: [expr.const], [except.handle]

Issue description:

Consider:

int main() {
  try {
    throw 0;
  } catch (...) {
    constexpr int x = []{
      try {
        throw;
      } catch (...)
        return 1;
      }
    }();
  }
}

This is not implementable to be well-formed, as throw; would be throwing a runtime exception within constant evaluation. However in the current wording I could not find anything that would disallow throw; in this context. My analysis is the following:

  1. There is a currently handled exception at the point of throw;

[except.handle]/10:

The exception with the most recently activated handler that is still active is called the currently handled exception.

In my reading "most recently activated handler" refers to sequencing within the current thread (maybe at least a "current thread" should be added to this sentence, but I digress).

I did not find anything that would suggest that sequencing the initialization of constexpr variables would be different to non-constexpr ones, even if they are (typically) not evaluated at runtime.

  1. There is nothing banning a throw; expression in [expr.const]

The blanket ban on throw expressions got removed with P3068, and got replaced with banning constructing exception objects that leak out from the constant evaluation.

There was no wording added to ban throw; when the currently handed exception was not thrown during the current constant evaluation.

Suggested resolution:

Add under [expr.const]/10.22:

  • a throw-expression ([expr.throw]) with no operand, unless the currently handed exception was thrown within the evaluation of E;

Activity

  1. t3nsor commented on Aug 28, 2025

    @t3nsor

    This wording presupposes the existence of a currently handled exception.

    Also, since [expr.const]/10.22 uses the "construction of an exception object" wording it seems like we should try to be consistent with that.

    That gives us: "unless there is a currently handled exception whose exception object was constructed within the evaluation of E".

  2. jensmaurer commented on Sep 10, 2025

    @jensmaurer
    Member
  3. changed the title [-][expr.const]: throw; in constant expression[/-] [+]CWG3059 [expr.const]: throw; in constant expression[/+] on Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions