template argument deductionを防止するdepedent_type
構造体を導入することができます。
あなたの例では
template <typename T>
struct dependent_type
{
using type = T;
};
struct A
{
void foo(int i) { /*...*/ };
template<typename T> void foo(typename dependent_type<T>::type t) { /*...*/ }
}
:あなたはa.foo(1.0)
Cを作りたい場合は
a.foo<int>(1); // calls the template
a.foo<double>(1.0); // calls the template
a.foo(1); // calls non-templated method
a.foo(1.0); // calls non-templated method (implicit conversion)
wandbox example
(この動作はcppreference >template argument deduction >non-deduced contextsに説明されている。)
ompilationエラーは、最初に過負荷を制限する必要があります。
template <typename T>
auto foo(T) -> std::enable_if_t<std::is_same<T, int>{}> { }
この技術はfoo
の上記過負荷のみint
引数取ります:暗黙的な変換(例えばをfloat
〜int
)は許可されません。これがあなたが望むものでないなら、TemplateRexの答えを考えてください。
wandbox example
(上記の制約機能により、a.foo<int>(1)
が呼ばれた2つのオーバーロードの間で好奇心旺盛な相互作用がある。私asked a question about it私はそれを導く基礎となるルールのわからないと。)
断然
これが必要ですか?関数をオーバーロードするのではなく、 'int'のテンプレートを特殊化するのはなぜでしょうか? –
@ChristianHacklは 'foo(double)= delete'を実行します。私の答えを見てください。 – TemplateRex