Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pointer to template function which class member

I have the following functions in class "C"

class C
{
  template<typename T> void Func1(int x);
  template<typename T> void Func2(int x);
};

template<typename T> void C::Func1(int x)
{
  T a(x);
}

template<typename T> void C::Func2(int x)
{
  T a(x);
}

The functions uses templates only in the implementation. The signature does not contain template parameters.

Is it possible to define pointers to such template functions?

I tried the following definition but it results compilation error.

typedef template<typename T> void (СSomeClass::*TFuncPtr)(int);

like image 289
Denis Solovov Avatar asked Nov 16 '11 14:11

Denis Solovov


1 Answers

Once instantiated, a member function template is just a normal member function, so you can use a normal member function pointer (best typedef'd like below):

typedef void (C::*mem_fun_ptr)(int);
mem_fun_ptr p = &C::Func1<Bar>;
//          IMPORTANT -- ^^^^^

The underlined part is the important part. You can't make a pointer to a function template, but you can make a pointer to an instantiated function template.

like image 195
Xeo Avatar answered Nov 15 '22 00:11

Xeo