處理模板函數的簡單示例。代碼編譯並按預期工作。但我的問題是爲什麼在「Cmp」和「Lit」中都需要「靜態」?否則,它不會編譯?模板函數採用模板參數
非常感謝!
template<class T> class Cmp{
public:
static int work(T a, T b) {
std::cout << "Cmp\n";
return 0;
}
};
template<class T> class Lit{
public:
static int work(T a, T b){
std::cout << "Lit\n" ;
return 0;
}
};
template<class T, class C>
int compare(const T &a, const T &b){
return C::work(a, b);
}
void test9(){
compare<double, Cmp<double> >(10.1, 20.2);
compare<char, Lit<char> >('a','b');
}
請注意,整個事情可以表達沒有任何類,只需要功能和功能模板。 –