【发布时间】:2015-07-06 12:37:22
【问题描述】:
我希望扩展 here 描述的功能,但对于成员函数,这种情况下的语法是什么?
另外,模板定义中的 (*) 是否取消引用函数指针以便编译器可以推断模板参数?
不胜感激!
谢谢
template <class F> struct ArgType;
template <class R, class T>
struct ArgType<R(*)(T)> {
typedef T type;
};
void f(int) {}
#include <type_traits>
#include <iostream>
int main() {
// To prove
std::cout << std::is_same< ArgType<decltype(&f)>::type, int >::value << '\n';
// To use
ArgType<decltype(&f)>::type a;
}
【问题讨论】:
-
模板定义中的
*只是函数指针语法的一部分。 -
这是一个函数指针,你需要一个成员函数指针,即
R(T::*)(Args...) -
分解成员函数类型通常在称为“成员函数(类型)特征”的情况下完成。 StackOverflow 上已经有几个实现; boost 也包含一个。
-
感谢大家的投入!
标签: c++ templates function-pointers member-function-pointers