Description
If a specification of a type of a function in a template argument uses the [[maybe_unused]]
attribute an error is raised - the initial [
is interpreted as a beginning of a lambda capture.
This description is quite a mouthful, so let me give an example. The following code:
//...
std::function<int([[maybe_unused]] int)> f = fun;
//...
Causes the following error:
<source>:9:24: error: expected variable name or 'this' in lambda capture list
9 | std::function<int([[maybe_unused]] int)> f = fun;
| ^
<source>:11:2: error: expected '>'
11 | }
| ^
<source>:9:18: note: to match this '<'
9 | std::function<int([[maybe_unused]] int)> f = fun;
| ^
<source>:11:2: error: expected unqualified-id
11 | }
| ^
<source>:11:2: error: expected '}'
<source>:8:1: note: to match this '{'
8 | {
| ^
4 errors generated.
Compiler returned: 1
The quoted error is for x86-64 clang 18.1.0 from godbolt, but it is almost identical for all versions (at least) since 12.0.0.
I agree the [[maybe_unused]]
attribute makes little sense in the presented context. However, at least to my understanding, the mentioned example is valid C++ syntax. I also believe a valid use case where it should be accepted is generated code. This, in fact, is how the issue was identified.
Other compilers (GCC and MSVC) correctly compile the code.