2

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?

Alex
  • 3,301
  • 4
  • 29
  • 43

2 Answers2

4

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);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
4

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.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312