2

I have a template class Tpl which takes a class and a pointer to member function as a parameter.

However when I attempt to pass a pointer to function that is actually inherited from the base class, I get an error could not convert template argument bla bla bla (see below)

#include <iostream>
using namespace std;

class Base
{
public:
    void func1 () {}
};

class Derived : public Base
{
public:
};

typedef void (Derived::*PF) ();

template <class T, void (T::*PF) ()>
class Tpl
{
public: 
};

int main()
{

    PF pf = &Derived::func1; // OK!

    Tpl <Derived, &Derived::func1> t; // Error :
    // could not convert template argument ‘&Base::func1’ to ‘void (Derived::*)()’  Tpl <Derived, &Derived::func1> t

    return 0;
}

Why is it that compiler can do implicit conversion when I assign pf, but not when I pass it as a template argument? And is there any way to overcome this other than doing static_cast?

cppalex
  • 537
  • 4
  • 11

0 Answers0