Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling lambda with template parameter list, without providing arguments

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?

like image 512
Bonita Montero Avatar asked Jun 14 '21 11:06

Bonita Montero


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>{});
like image 75
HolyBlackCat Avatar answered Oct 18 '22 00:10

HolyBlackCat