1

I am trying to pass a function as parameter to class, but getting error as use of class template requires template argument list.

#include <iostream>

void f_global ()
{
    std::cout << "In function global\n";
}

template<typename F>
class A
{
public:
    F f1;
    A(const int var1, F fun1) : a(var1), f1(fun1) {}
    void f2();

private:
    int a;

};

void A::f2()
{
    std::cout << "in A::f2()\n";
}
int main()
{
    A obj(5,f_global);
    obj.f2();
}

I am trying to use auto template deduction feature while passing function from obj What are template deduction guides and when should we use them?

ewr3243
  • 397
  • 3
  • 19

1 Answers1

4

You are simply missing the template parameter in the definition of A::f2():

template<typename F>
void A<F>::f2()
{
    std::cout << "in A::f2()\n";
}

When the member function definition is outside of the class definition, then the above syntax is necessary to provide proper template instantiation.

Skrino
  • 520
  • 2
  • 12