以下のコードスニペットでは、
template<typename T1>
void func(T1& t)
{
cout << "all" << endl;
}
template<typename T2>
void func(T2 &t)
{
cout << "float" << endl;
}
// I do not want this
// template<> void func(float &t)
int main()
{
int i; float f;
func(i); // should print "all"
func(f); // should print "float"
return 0;
}
float以外のタイプを渡すと「all」が出力され、floatを渡すと「float」と出力されるテンプレートを変更したいと思います。テンプレートの特殊化は必要ありません。代わりに、入力タイプに基づいてそれに応じて動作する部分的な特殊化があります。どうすればいいですか。前もって感謝します。
さて、私が現在直面しているシナリオは、次のように定義する必要があります。
template<typename T1>
void func(T1 &t)
{
cout << "t1" << endl;
}
template<typename T2>
void func(T2 &t)
{
cout << "t2" << endl;
}
次の呼び出しは「t2」を出力する必要があります
func(int) // print "t2"
func(float) // print "t2"
func(string) // print "t2"
次の呼び出しは「t1」を出力する必要があります
func(char) // print "t1"
func(xyz) // print "t1"
...
func(abc) // print "t1"
上記のようなある種のグループ化では、部分的な特殊化の実装を呼び出す必要があり、デフォルトの実装を呼び出す必要があります。