jeudi 21 juillet 2016

Matching function for lambda template

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?

Aucun commentaire:

Enregistrer un commentaire