Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parametrize template by function name c++11

Having the following functions

void print(int a) { cout << a << endl; }
void print(std::string a) { cout << a << endl; }

You can do the following template

template <class T> void printT(T a) { print(a); }

Is there some mechanism to parametrize the function name? Something like this:

template <class T, class F> void anyT(T a) { F(a); }

I don't need to be a function template, just some mechanism to achieve the same.

like image 512
Edwin Rodríguez Avatar asked Dec 14 '15 15:12

Edwin Rodríguez


1 Answers

Yes, you could pass the caller as a function pointer that takes as input T like below:

template <class T> void anyT(T a, void(*f)(T)) {
  f(a);
}

Live Demo

like image 161
101010 Avatar answered Oct 26 '22 23:10

101010