Skip to content

[[maybe_unused]] attribute used inside a template argument specifying a type of a function causes a compilation error #108095

Open
@jarzec

Description

@jarzec

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second partydiverges-from:edgDoes the clang frontend diverge from edg compilerdiverges-from:gccDoes the clang frontend diverge from gcc on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions