Skip to content

Code using a lambda to capture a reference to a function rejected with could not match (null template argument) against (null template argument)Β #84961

@abbeyj

Description

@abbeyj

The following code is accepted by other compilers but rejected by clang. The error message is a bit weird.

template<typename T>
void g(const T& t) {
}

template<typename T>
void f(const T& t) {
  [t] { g(t); };
}

void h() {
}

int main() {
  f(h);
}

Output:

<source>:7:9: error: no matching function for call to 'g'
    7 |   [t] { g(t); };
      |         ^
<source>:7:7: note: while substituting into a lambda expression here
    7 |   [t] { g(t); };
      |       ^
<source>:14:3: note: in instantiation of function template specialization 'f<void ()>' requested here
   14 |   f(h);
      |   ^
<source>:2:6: note: candidate template ignored: could not match (null template argument) against (null template argument)
    2 | void g(const T& t) {
      |      ^

Changing the call g(t) to be either g<T>(t) or g<void()>(t) changes the error message to:

note: candidate function template not viable: 1st argument ('void (const()') would lose const qualifier

Note that the type mentioned here (void (const()) has unbalanced parentheses.

Workaround: change the lambda capture from [t] to [t=t].

This code was accepted by clang 3.0.0 but is rejected by all version from 3.1 to the latest trunk currently available on Godbolt.

Godbolt link: https://godbolt.org/z/d87rPjja4

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"lambdaC++11 lambda expressions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions