#include <cstdio> // nobody is foo template <typename T> struct IsFoo { static const bool value = false; }; // ... except for `double` template <> struct IsFoo<double> { static const bool value = true; }; // ... and `int` template <> struct IsFoo<int> { static const bool value = true; }; //---------------------------------------------------- template <bool cond, typename T> struct RestrictTo { }; template <typename T> struct RestrictTo<true, T> { typedef T Type; }; //---------------------------------------------------- template <typename T> typename RestrictTo<IsFoo<T>::value, void>::Type print(const T &) { std::printf("Is foo\n"); } template <typename T> typename RestrictTo<! IsFoo<T>::value, void>::Type print(const T &) { std::printf("Is not foo\n"); } int main() { print(false); print('c'); print(42); print(1.2f); print(1.2); } |