A couple of related questions for C++ standard gurus.
The incoming C++20 introduces template lambdas (P0428R2).
So instead of
auto x = [](auto x, auto y){ return x+y; };
we can specify the template parameter as follows
auto x = []<typename T>(T x, T y){ return x+y; };
So far, so good.
First question: can explicit template parameters, in template lambdas, only be deduced from arguments, or is it possible to add non-deduced template arguments?
Reading P0428r1 I don't see any explicit limitations but, also, I don't see examples of non-deduced template arguments.
In first approximation I suppose that non-deduced template arguments are legal because I see that the following silly code
int main()
{
[]<int = 0>(){ }();
}
compiles and runs with both g++ (10.0.0 head) and clang++ (10.0.0 head).
Supposing that non-deduced template parameters are allowed, the second question is: how can I call a template lambda while providing a template parameter?
By example: given the following template lambda
auto x = []<std::size_t I>(auto t){ return std::get<I>(t); };
Is there some syntax for specifying the template parameter I
when invoking such lambdas without explicitly naming operator()
?
I've tried with
x<0u>(y);
but the <
is interpreted as a relational operator.
I've tried simply adding template
x template <0u>(y);
but it doesn't work.
See Question&Answers more detail:os