大家来看看这个C++中的template到底是怎么一回事!太奇怪了。
下面这么写是正确的,gcc 4.3 是可以的。
#include <iostream>
using namespace std;
typedef int (*tfun)(int);
template<tfun function>
struct S {
void show(){
cout<<function(100)<<endl;
}
};
int fun(int n) {
return n;
}
int main2() {
S<fun> t;
t.show();
return 0;
}
请大家解释解释这个模板的使用到底是怎么一回事,此外,我如果想把show这个成员方法的定义拿到类外部,该如何写呢?
template<tfun function>
struct S {
void show();
};
?????????//这里到底该怎么写?
void S::show(){
cout<<function(100)<<endl;
}