这是我想做的:
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;
它工作得很好(如所描述的),但它引入了一种额外的帮助器类型。
但是我可以在没有额外类型的情况下获得相同的结果吗?
您可以通过将 T 放在不可推导的上下文中(位于::
),并使用std::common_type http://en.cppreference.com/w/cpp/types/common_type from <type_traits>
.
example:
template <typename T> void f(typename std::common_type<T>::type obj) {std::cout << obj;}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)