Bug 117061 - Error on use of parameter in lambda outside function body
Summary: Error on use of parameter in lambda outside function body
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda
Depends on:
Blocks: c++-lookup, c++-name-lookup
  Show dependency treegraph
 
Reported: 2024-10-10 13:58 UTC by eczbek.void
Modified: 2025-03-28 13:19 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description eczbek.void 2024-10-10 13:58:07 UTC
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
Comment 1 Andrew Pinski 2024-10-10 15:39:05 UTC
Accepts:
* clang
* MSVC

Rejects:
* GCC
* EDG
Comment 2 eczbek.void 2024-10-10 15:58:52 UTC
Clang errors on a similar-looking piece of code:

```
void foo(auto x) noexcept(noexcept([x] { x; })) {}
```
Comment 3 eczbek.void 2025-01-19 18:55:03 UTC
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
Comment 4 eczbek.void 2025-03-04 02:06:21 UTC
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; }; }) {}
      |                                         ^
```
Comment 5 eczbek.void 2025-03-28 13:19:09 UTC
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

:(