Public Types | |
typedef T | type |
true
first template argument declares a local typedef type
to the second template argument. It is used in order to construct constraints on template arguments in template (and member template) functions. The negative specialization is missing.Here's how the trick works, called SFINAE (substitution failure is not an error): The C++ standard prescribes that a template function is only considered in a call, if all parts of its signature can be instantiated with the template parameter replaced by the respective types/values in this particular call. Example:
* template <typename T> * typename T::type foo(T) {...}; * ... * foo(1); *
The idea is then to make the return type un-instantiatable if certain constraints on the template types are not satisfied:
* template <bool, typename> struct constraint_and_return_value; * template <typename T> struct constraint_and_return_value<true,T> { * typedef T type; * }; *
* template <typename> * struct int_or_double { static const bool value = false;}; * template <> * struct int_or_double<int> { static const bool value = true; }; * template <> * struct int_or_double<double> { static const bool value = true; }; *
* template <typename T> * typename constraint_and_return_value<int_or_double<T>::value,void>::type * f (T); *