Skip to content

CWG3035 [class.union.anon] Are lambda expressions disallowed in anonymous unions? #706

Description

@hubert-reinterpretcast

Full name of submitter (unless configured in github; will be published with the issue): Hubert Tong

Reference (section label): class.union.anon

Link to reflector thread (if any): N/A

Issue description:
https://eel.is/c++draft/class.union.anon#1.sentence-3 says:

Nested types, anonymous unions, and functions shall not be declared within an anonymous union.

A lambda expression declares a closure type (https://eel.is/c++draft/expr.prim.lambda#closure-2).

Does that mean that lambda expressions are disallowed in anonymous unions?

Only EDG generates a diagnostic for https://godbolt.org/z/xf96EbKTn:

static union {
  int x = [] { return 42; }();
};

Suggested resolution:

Nested types, including closure types and anonymous unions, and functions shall not be declared within an anonymous union.

Activity

  1. eisenwave commented on May 22, 2025

    @eisenwave
    Member

    I think the wording is fairly clear on this, although it is surprising that IILEs wouldn't be okay.

    I doubt that the current behavior is intentional, and maybe a better approach would be to ask EWG to exclude closure types, effectively standardizing what Clang, GCC, and MSVC already do. If we don't do that, the resolution looks correct.

  2. jensmaurer commented on Jul 26, 2025

    @jensmaurer
    Member
  3. changed the title [-][class.union.anon] Are lambda expressions disallowed in anonymous unions?[/-] [+]CWG3035 [class.union.anon] Are lambda expressions disallowed in anonymous unions?[/+] on Jul 26, 2025
  4. hubert-reinterpretcast commented on Nov 11, 2025

    @hubert-reinterpretcast
    MemberAuthor

    We can probably fix cplusplus/draft#4938 at the same time while we are at it.

  5. jensmaurer commented on Nov 20, 2025

    @jensmaurer
    Member

    We can probably fix cplusplus/draft#4938 at the same time while we are at it.

    @hubert-reinterpretcast Do you mean something like this?

    Nested types, including closure types (7.5.6.2 [expr.prim.lambda.closure]) and anonymous unions, and functions other than implicitly-declared special member functions shall not be declared within an anonymous union....

  6. hubert-reinterpretcast commented on Nov 21, 2025

    @hubert-reinterpretcast
    MemberAuthor

    @hubert-reinterpretcast Do you mean something like this?

    Nested types, including closure types (7.5.6.2 [expr.prim.lambda.closure]) and anonymous unions, and functions other than implicitly-declared special member functions shall not be declared within an anonymous union....

    Yes. Although, with the discussion happening around https://cplusplus.github.io/CWG/issues/3130.html, it seems we should probably hold off on doing anything about implicitly-declared special member functions here.

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