有没有办法让funci
成为非静态的?请显示声明,初始化和调用。
另外,有没有办法摆脱函数调用中的冗余<decltype(i)>
?
最后,我不确定函数指针是什么的准确名称。我很好奇是否有人争论标签是否准确或应该使用什么标签。
#include <iostream>
using namespace std;
template <typename T>
void makeit(T t){
cout << typeid(t).name() << " func" << endl;
}
struct A{
using Func = void(*)(int);
const Func func[1] = {&makeit<int>};
template <typename T>
using Funci = void (*)(T);
//works fine
template <typename T>
const static Funci<T> funci[1];
//works with out-of-class definition below
//template <typename T>
//const Funci<T> funci2[1] = {&makeit};
//compiler error: member 'funci2' declared as template
};
template <typename T>
const typename A::template Funci<T> A::funci[] = {&makeit};
int main(int argc, const char * argv[]) {
A ay;
int i;
double d;
(*ay.func[0])(i);
(*A::funci<decltype(d)>[0])(d);
//(*ay.funci2<decltype(d)>[0])(d);
return 0;
}
输出:
i func
d func