#include <type_traits> #include <iostream> using namespace std; template<typename T> void doo(T t, true_type) { cout << t << " is int!\n"; } template<typename T> void doo(T t, false_type) { cout << t << " is not int!\n"; } template<typename T> struct not_int_helper : public false_type {};
//全特化 template<> struct not_int_helper<int> : public true_type { }; template<typename T> struct not_int_simple: public not_int_helper<T>::type { }; /**完整版!**/ //template<typename T> //struct not_int_simple: public not_int_helper<typename remove_cv<T>::type>::type //{ }; template<typename T> T do_foo(T t){ doo(t, not_int_simple<T>()); } int main() { do_foo<int>(3); do_foo<long>(2); do_foo<short>(1); }
仿照,struct is_integral 实现struct not_int。