8
這是我想做的事:更好的方法來禁用函數的基於參數的模板參數演繹?
template <typename T> void f(DisableDeduction<T> obj) {std::cout << obj;}
// Here DisableDeduction<T> aliases T, but in a such way
// that would prevent compiler from deducing T based
// on provided argument.
/* ... */
f<int>(1); // Works.
f(1); // Error, can't deduce template parameter based on argument.
這是我當前如何實現它:
template <typename T> struct DisableDeduction_Internal {using type = T;};
template <typename T> using DisableDeduction = typename DisableDeduction_Internal<T>::type;
它完美(如上所述),但它引入了一個額外的輔助型。
但是,我可以達到同樣的結果沒有額外的類型?
'的std :: enable_if_t'? –
Jarod42
@ Jarod42,你會介意把它變成一個有效的例子嗎? –