You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generated C++ for casts to unnamed closure types and for closure-type condition variables is ill-formed #576
The C++-generating back end can name a closure type only through a typedef decltype(v) __Tn that it puts out after the declaration of a variable v of that type. Two problems follow:
A cast to a closure type for which no such typedef was put out is generated with the closure type written as if it were a class name, for example class __T1{...}, which is ill-formed. Such a cast can only be written with auto (C++23): auto{x} or auto(x), where x is a lambda-expression, a call returning a closure object, or a closure variable for which no typedef is put out (for example, one declared in a for init).
For a variable of closure type declared in the condition of an if, switch, or while statement, or in a range-based for statement, the typedef is put out inside the declaration, which is also ill-formed. The second problem doesn't need C++23.
The front end itself accepts the source. This came up while looking at PR #575 (for #572).
Reproducing Source Code
int i = 3;
template<classT> intsize(T&&) { returnsizeof(T); }
int x = auto{[](int a) { return a; }}(2); // 1int y = size(auto{[]{}}); // 2intf() { int j = 1; returnauto{[&] { return i + j; }}(); } // 3autog() { return [](int a) { return a; }; }
int z = auto{g()}(2); // 4inth() {
for (auto m = [n = 0]() mutable { return ++n; }; ; )
returnauto{m}() + auto(m)(); // 5
}
intk() {
if (auto m = [] { return1; }) returnm(); // 6return0;
}
Command-line Options
cpfe-cp --c++23 --gen_c_file_name t.int.c t.cpp
Type of Issue
[Back End] There is a problem with the generated C++ code.
Additional Details
GCC and Clang accept the source, as does cpfe --c++23 (without C++ generation).
The generated C++ for the marked lines is:
int x = class__T1{[](int a) { return a; } }(2);
int y = size(class__T2{[]{ } });
intf() { int j = 1; returnclass__T3{[&]{ return i + j; } }(); }
int z = class__T4{g()}(2);
for (auto m = [n = 0]() mutable { return ++n; } ; ;) { returnclass__T5{m}() + ((__T5)(m))(); }
if (auto m = []{ return1; } typedefdecltype(m) __T6;) { returnm(); }
which GCC rejects (expected primary-expression before 'class', 'typedef' invalid in condition, ...). A range-based for gives, e.g., for (auto m typedef decltype(m) __T1; : r).
Related forms are generated correctly:
A cast of a closure variable with a typedef, like auto m = [](int a) { return a; }; auto{m}(1), becomes __T1{m}(1).
The parenthesized form of a prvalue operand, like auto([](int a) { return a; })(2), becomes ([](int a) { return a; } )(2).
An initializer like auto g = auto{[]{ return 1; }}; becomes auto g = []{ return 1; } ;.
The problem already occurs on main. It shows up in the edg_x86_64_cp and edg_x86_32_cp test configurations, which compile the generated C++.
changed the title [-]Generated C++ for a braced cast of a lambda-expression names the closure type[/-][+]Generated C++ for casts to unnamed closure types and for closure-type condition variables is ill-formed[/+]on Nov 20, 2026
Activity
[-]Generated C++ for a braced cast of a lambda-expression names the closure type[/-][+]Generated C++ for casts to unnamed closure types and for closure-type condition variables is ill-formed[/+]