次のデモコードがあります。
template <int i, typename T, typename U>
T func(const U &t){return i * t;}
template <int i, typename T>
T func(const T &t){return 2 * i * t;}
int main()
{
return func<1, int>(1);
}
これは私の実際のコードの煮詰めたバージョンなので、役に立たないように見えますが、問題を示すには十分なはずです:
In function ‘int main()’:
11:23: error: call of overloaded ‘func(int)’ is ambiguous
11:23: note: candidates are:
2:3: note: T func(const U&) [with int i = 1, T = int, U = int]
5:3: note: T func(const T&) [with int i = 1, T = int]
したがって、自動型推論 (テンプレート パラメーター U の場合) が、テンプレート関数の正しいバージョン (パラメーターが 2 つしかないバージョン) を選択するという私の興味を妨げることは明らかです。
少し異なることを行う基本的なテンプレートと特殊なテンプレートを両方のバージョンに用意する必要があります。
質問は次のとおりです。この時点で型を自動的に推論しないようにコンパイラに指示する可能性はありますか (たとえば、パラメータが 2 つしかないテンプレートを使用するなど)。