考虑在c ++中实现例程的三种方法:通过仿函数,成员函数和非成员函数.例如,
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class FOO
{
public:
void operator() (string word) // first: functor
{
cout << word << endl;
}
void m_function(string word) // second: member-function
{
cout << word << endl;
}
} FUNCTOR;
void function(string word) // third: non-member function
{
cout << word << endl;
}
Run Code Online (Sandbox Code Playgroud)
现在考虑一个模板函数来调用上面的三个函数:
template<class T>
void eval(T fun)
{
fun("Using an external function");
}
Run Code Online (Sandbox Code Playgroud)
FOO::m_function通过eval 调用的正确方法是什么?
我试过了:
FUNCTOR("Normal call"); // OK: call …Run Code Online (Sandbox Code Playgroud) c++ templates member-function-pointers functor template-function