传递成员函数作为函数模板的参数 [英] passing member-function as argument to function-template

查看:275
本文介绍了传递成员函数作为函数模板的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑三种方法来实现c ++中的例程:通过函子,成员函数和非成员函数。例如,

Consider three ways to implement a routine in c++: through functors, member functions, and non-member functions. For example,

#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;
}

现在考虑一个模板函数来调用上面的三个函数:

Now consider a template-function to call the three functions above:

template<class T>
void eval(T fun)
{
  fun("Using an external function");
}

什么是正确的方法调用 FOO :: m_function 通过eval?
我尝试:

What is the proper way to call FOO::m_function through eval? I tried:

FUNCTOR("Normal call");               // OK: call to ‘void FOO::operator()(string)‘
eval(FUNCTOR);                        // OK: instantiation of ‘void eval(T) [with T = FOO]’

function("Normal call");              // OK: call to ‘void function(string)’
eval(function);                       // OK: instantiation of ‘void eval(T) [with T = void (*)(string)]’

FUNCTOR.m_function("Normal call");    // OK: call to member-function ‘FOO::m_function(string)’
eval(FUNCTOR.m_function);             // ERROR: cannot convert ‘FOO::m_function’ from type
                                      //        ‘void (FOO::)(std::string) {aka void (FOO::)(string)}’
                                      //        to type ‘void (FOO::*)(std::basic_string<char>)’
                                      // In instantiation of ‘void eval(T) [with T = void (FOO::*)(string)]’:
                                      // ERROR: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘fun (...)’, e.g. ‘(... ->* fun) (...)’


推荐答案

指向成员函数的指针和指向函数的指针是两个不同的野兽。前者采用隐式的第一个参数, this 指针,或者换句话说,指向要调用成员函数的实例的指针。

A pointer to member function and a pointer to function are two different beasts. The former takes an implicit first argument, the this pointer, or in other words, a pointer to the instance on which the member function is to be invoked on.

通常,为了能够将成员函数作为可调用对象传递,您 bind 上调用它的实例,然后使用 占位符 表示将稍后传递给可调用方。

Typically, in order to be able to pass the member function as a callable object, you bind the instance on which it is to be invoked on, and then use placeholders to indicate arguments that will be passed to the callable later. In your case

eval(std::bind(&FOO::m_function, &FUNCTOR, std::placeholders::_1));

上面 bind 的第一个参数是指向要调用的成员函数的指针,第二个是指向您要调用的 FOO 实例的指针 m_function 。最后一个是一个占位符,表示在调用成员函数时应该使用 bind 创建的可调用方传递的第一个参数。

The first argument to bind above is the pointer to member function that you want to invoke, and the second is a pointer to the FOO instance on which you want to invoke m_function. The last one is a placeholder that indicates the first argument passed to the callable created by bind should be used when calling the member function.

另一种方法是将一个lambda表达式传递给 eval ,它需要一个 char const * (或 std :: string const& )参数并调用成员函数。

Another way to do this is to pass a lambda expression to eval that takes a char const * (or std::string const&) argument and calls the member function.

eval([](char const *c) { FUNCTOR.m_function(c); });

现场演示

这篇关于传递成员函数作为函数模板的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆