Calling lambda with template parameter list, without providing arguments

Viewed 82

Can anyone tell me whether this ...

#include <memory>

using namespace std;

int main()
{
    auto f = []<typename T>() -> T
    {
        return 123;
    };
    f.operator ()<int>();
}

... is the only way to call the C++20 generic lambda without arguments?

1 Answers

Yes, it's the only way to call them.

If the lambda can be changed, consider using a tag:

template <typename> struct tag {};

auto f = []<typename T>(tag<T>) {...};
f(tag<int>{});
Related