Lambda functions with template parameters, not in function parameters

折月煮酒 提交于 2019-11-30 13:52:06
Barry

This is easier to understand if you consider what the equivalent class type looks like to your get1:

struct get1_t {
    template <int B> operator()() const { return B; }
};

get1_t get1;

get1<5>(); // error

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 if get1 were a variable template). In order to provide the template parameter for the call operator, you have to do that directly:

get1.operator()<5>(); // ok

Or restructure the call operator to take something deducible:

template <int B> struct constant { };
get1(constant<5>{});

Or restructure the whole thing to actually be the variable template that it looks like it is:

template <int B>
auto get1 = [] { return B; };

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!