質問はコードです。 2番目の関数が1番目の関数よりも特別なように見えます。次のコードでより一般的なものが呼び出されるのはなぜですか?他の機能を使用するにはどうすればいいですか?関数テンプレートのオーバーロード解決
template <typename T>
class Base{
public:
Base(){}
void print() const {cout<<"Base class"<<endl;}
};
template <typename T>
class Derived :public Base<T>{
public:
Derived() {}
void print() const {cout<<"Derived class"<<endl;}
};
template <typename T>
void func(T x){ // <----- Why is function is called?
x.print();
cout<<"in func(T)"<<endl;
}
template <typename T>
void func(const Base<T>& x){
x.print();
cout<<"in func(Base<T>)"<<endl;
}
int main() {
Base<int> b;
Derived<int> d;
func(d);
return 0;
}
私は、Derivedオブジェクトを関数に渡していることに注意してください。
この投稿はあなたを助けることができます:http://stackoverflow.com/questions/22411482/c-template-functions-overload-resolution @NikosAthanasiouの答えを見てください。 – chema989
可能な複製:http://stackoverflow.com/questions/31563580/c-templated-function-overloading-rules?lq=1 – sameerkn