C ++方法名称作为模板参数 [英] C++ method name as template parameter

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

问题描述

如何使方法名称(此处 some_method )为模板参数?

How do I make the method name (here some_method) a template parameter?

template<typename T>
void sv_set_helper(T& d, bpn::array const& v) {
  to_sv(v, d.some_method());
}


推荐答案

a模板标识符参数,因此您不能将名称作为参数传递。然而你可以把一个成员函数指针作为参数:

There is no such thing as a 'template identifier parameter', so you can't pass names as parameters. You could however take a member function pointer as argument:

template<typename T, void (T::*SomeMethod)()>
void sv_set_helper(T& d, bpn::array const& v) {
   to_sv(v, ( d.*SomeMethod )());
}

假设函数有一个 void 返回类型。您将这样调用它:

that's assuming the function has a void return type. And you will call it like this:

sv_set_helper< SomeT, &SomeT::some_method >( someT, v );

这篇关于C ++方法名称作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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