Why does the first call not compile?
auto get1 = []<int B>() { return B; };
auto get2 = []<typename B>(B b) { return b; };
int main()
{
get1<5>(); // error: no match for operator<
get2(5); // ok
}
The reason I use this, is an expression repeated many times in code.
Of course I can use a real function template, but just I am curious WHY.
This is easier to understand if you consider what the equivalent class type looks like to your
get1
:You're trying to provide an explicit template parameter to the call operator, but syntactically you're doing what looks like providing template parameters for
get1
itself (i.e. as ifget1
were a variable template). In order to provide the template parameter for the call operator, you have to do that directly:Or restructure the call operator to take something deducible:
Or restructure the whole thing to actually be the variable template that it looks like it is:
Now,
get1<5>
is itself a lambda, that you're invoking. That is, rather than a lambda with a call operator template we have a variable template lambda that is itself not a template.