I would like to call a method that receives two templates like this code:
template<class T>
class Test {
T t;
public:
Test(T v):t(v){}
template<class FUNC, size_t N>
T Func(FUNC&& fn) {
T v = 0;
for (size_t i = 0; i < N; i++)
v += fn(i);
return v;
}
};
int main(int argc, char **argv) {
Test<int> t(2);
auto l = [](size_t i) {return 2*i;};
int v = t.Func<decltype(l), 3>(l);
return 0;
}
But I got this error:
no matching function for call to ‘Test<int>::Func(main(int, char**)::<lambda(size_t)>&)’
int v = t.Func<decltype(l), 3>(l);
How to macth a method like that?
I would suggest you change the order of the template arguments, and let the compiler auto-deduce the FUNC
type.
Like
template<size_t N, class FUNC>
T Func(FUNC fn) { ... }
And call it as
int v = t.Func<3>(l);
You should change the order of the template parameters so that only N
needs to be specified and FUNC
can just be deduced:
template <size_T N, class FUNC>
T Func(FUNC&& fn);
t.Func<3>(l);
If you need to explicitly specify FUNC
, it should be decltype(l)&
so that by the reference-collapsing rules FUNC&&
becomes decltype(l)&
and can bind to the lvalue l
. If you specify FUNC
as just decltype(l)
without the reference, the parameter becomes decltype(l)&&
which can't bind to an lvalue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.