C++ C++;方法名作为模板参数
如何将方法名(此处C++ C++;方法名作为模板参数,c++,templates,C++,Templates,如何将方法名(此处some\u method)作为模板参数 template<typename T> void sv_set_helper(T& d, bpn::array const& v) { to_sv(v, d.some_method()); } 模板 void sv_set_helper(T&d、bpn::数组常量和v){ to_sv(v,d.某些_方法()); } 没有“模板标识符参数”,因此不能将名称作为参数传递。但是,您可以将成员函数指针作为参数
some\u method
)作为模板参数
template<typename T>
void sv_set_helper(T& d, bpn::array const& v) {
to_sv(v, d.some_method());
}
模板
void sv_set_helper(T&d、bpn::数组常量和v){
to_sv(v,d.某些_方法());
}
没有“模板标识符参数”,因此不能将名称作为参数传递。但是,您可以将成员函数指针作为参数:
template<typename T, void (T::*SomeMethod)()>
void sv_set_helper(T& d, bpn::array const& v) {
to_sv(v, ( d.*SomeMethod )());
}
这里有一个简单的例子
#include <iostream>
template<typename T, typename FType>
void bar(T& d, FType f) {
(d.*f)(); // call member function
}
struct foible
{
void say()
{
std::cout << "foible::say" << std::endl;
}
};
int main(void)
{
foible f;
bar(f, &foible::say); // types will be deduced automagically...
}
#包括
模板
空心钢筋(T&d,f型){
(d.*f)(;//调用成员函数
}
结构缺陷
{
void say()
{
std::cout谢谢!将T:::*SomeMethod放在括号中可以使这个解决方案起作用。+1谢谢,你在K-ballo之前就有了*f
部分,但他的答案是第一个。
#include <iostream>
template<typename T, typename FType>
void bar(T& d, FType f) {
(d.*f)(); // call member function
}
struct foible
{
void say()
{
std::cout << "foible::say" << std::endl;
}
};
int main(void)
{
foible f;
bar(f, &foible::say); // types will be deduced automagically...
}