This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2026-02-06


3035. Lambda expressions in anonymous unions

Section: 11.5.2  [class.union.anon]     Status: tentatively ready     Submitter: Hubert Tong     Date: 2025-05-12

(From submission #706.)

Consider:

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

According to 11.5.2 [class.union.anon] paragraph 1, this is ill-formed, because the closure type is declared as a nested type of the anonymous union (7.5.6.2 [expr.prim.lambda.closure] paragraph 3, but some implementations do not yield a diagnostic.

On the other hand, the code shown in the example is reasonable and ought to be allowed.

Proposed resolution (approved by CWG 2026-02-06):

Change in 11.5.2 [class.union.anon] paragraph 1 as follows:

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