Why does C++20 not allow to call a generic lambda with an explicit type?

Viewed 206
auto f1 = []<typename T>(T) { return T{}; };
auto f2 = []<typename T>()  { return T{}; };

int main()
{
    f1(1);     // ok
    f2<int>(); // err: expected primary-expression before 'int'
}

Why does C++20 not allow to call a generic lambda with an explicit type?

1 Answers

The correct syntax to invoke overloaded template operator () function supplying template parameters is

auto f1 = []<typename T>(T) { return T{}; };
auto f2 = []<typename T>()  { return T{}; };

int main()
{
    f1(1);     // ok
    f2.operator ()<int>(); // Ok
}

https://godbolt.org/z/YzY7njPGh

Related