This code causes an error: https://godbolt.org/z/3EdfzMqr9 ``` void foo(int x) noexcept(noexcept([&x] { x; })) {} int main() { foo(3); } ``` <source>: In lambda function: <source>:1:43: error: use of parameter outside function body before ';' token 1 | void foo(int x) noexcept(noexcept([&x] { x; })) {} | ^ Compiler returned: 1
Accepts: * clang * MSVC Rejects: * GCC * EDG
Clang errors on a similar-looking piece of code: ``` void foo(auto x) noexcept(noexcept([x] { x; })) {} ```
Another test case: https://godbolt.org/z/98joW6saj ``` auto f = [x = 0] noexcept(noexcept(x)) { return x; }; ``` <source>:1:36: error: 'x' was not declared in this scope 1 | auto f = [x = 0] noexcept(noexcept(x)) { return x; }; | ^ Compiler returned: 1
Constructors too :( ``` template<int> struct S { S(int x) requires(requires { [x] { x; }; }) {} }; ``` ``` <source>: In lambda function: <source>:3:41: error: use of parameter outside function body before ';' token [-Wtemplate-body] 3 | S(int x) requires(requires { [x] { x; }; }) {} | ^ ```
This is probably invalid, because lambdas can only have captures if they are in block scope, and the noexcept specifier/requires clause are outside it https://eel.is/c++draft/expr.prim.lambda.capture#3 https://eel.is/c++draft/dcl.fct.def#nt:function-body https://eel.is/c++draft/basic.scope.block#1 :(