C ++ template metaprogramming 7: traits class for Type judgment, template traits
2. traits class used to determine the type
The is_void class can be used to determine the type of template parameters. It is easy to use. You can also use: value directly.
Cout <std: is_void <void>: type: value <endl; cout <std: is_void <bool>: type: value <endl;
However, the implementation is interesting. The special feature can be used to inherit different classes. The clang ++ implementation code is not found here. Use boost code instead.
Template <typename T> struct is_void: public false_type {}; template <> struct is_void <void>: public true_type {};
The c ++ 11 standard also supports is_pointer, is_floating_point, is_array, is_lvalue_reference, is_rvalue_reference, is_member_object_pointer, struct, is_enum, is_union, is_class, is_function. The implementation code is very simple. Here is another example:
Template <typename T> struct is_pointer: public false_type {}; template <typename T> struct is_pointer <T *>: public true_type {};