Type Traits of vc8 is used to obtain the type information during compilation. In the boost library, many strange methods are used to detect the type information. It is mainly a template-specific mechanism. For example, whether two types have been: template <typename T1, typename T2>
Struct is_same
{
Static const bool value = false;
}; Template <typename T>
Struct is_same <t, t>
{
Static const bool value = true;
}; A general version of the template, a special, can distinguish whether the input type is consistent: int * P = is_same <int, long >:: value; // compiled successfully, because false is a valid NULL pointer constant int * P = is_same <int, int>: value; // compilation error, because true is not a pointer. What if I want to check whether it is an integer? Template <typename T>
Struct is_integral
{
Static const bool value = false;
}; Template <> struct is_integral <bool> {static const bool value = true;}; Template <> struct is_integral <char> {static const bool value = true ;}; template <> struct is_integral <wchar_t> {static const bool value = true ;}; template <> struct is_integral <signed Char >{ static const bool value = true ;}; template <> struct is_integral <unsigned char> {static const bool value = true ;}; template <> struct is_integral <short >{ static const bool value = true ;};
Template <> struct is_integral <unsigned short> {static const bool value = true ;};
Template <> struct is_integral <int> {static const bool value = true ;};
Template <> struct is_integral <unsigned int> {static const bool value = true ;};
Template <> struct is_integral <long> {static const bool value = true ;};
Template <> struct is_integral <unsigned long> {static const bool value = true ;};
... Another omission is found, that is, if the type is not consistent with the original type, then is_interal <const int>: value is no longer true. Therefore, you need to fix this bug, also Special: Template <> struct is_integral <const int> {static const bool value = true ;}; template <> struct is_integral <volatile int> {static const bool value = true ;}; template <> struct is_integral <const volatile int> {static const bool value = true ;}; well, let's expand the above three times. These operations are easy to implement. complicated operations need to be calculated based on some rules. If this is not the case, it is not the case or the case ......, It is something, for example, determining whether it is a custom class type: Not a built-in type
Not an array
Not a pointer
Not reference
Not a function
Not a function pointer
If a member function is not a member function pointer, it must be of the class type. Trouble? What's more troublesome. For example, how can we determine whether it is Enum or union? There are methods, but they are becoming more and more complex. For example, how to determine whether the pod type is the pod type, especially the pod type, is of great help to the performance of the template library, because the pod type can be directly moved using memcpy without the need for structure and analysis. Unfortunately, some of the above rules cannot be launched. In fact, this information is inside the compiler. Instead of secretly detecting this information, it is not only slow but also error-prone. It is better to ask the compiler directly, it is said that the SGI compiler has long implemented some built-in type traits support compiler, now the good news, VC 2005 also supported: http://msdn2.microsoft.com/en-us/library/ms177194 (vs.80 ). aspx, however, VC 2005 only supports some template-specific mechanisms that are difficult to detect. The list is as follows: __has_assign (type)
_ Has_copy (type)
_ Has_finalizer (type)
_ Has_nothrow_assign (type)
_ Has_nothrow_constructor (type)
_ Has_nothrow_copy (type)
_ Has_trivial_assign (type)
_ Has_trivial_constructor (type)
_ Has_trivial_copy (type)
_ Has_trivial_destructor (type)
_ Has_user_destructor (type)
_ Has_virtual_destructor (type)
_ Is_abstract (type)
_ Is_base_of (base, derived)
_ Is_class (type)
_ Is_convertible_to (from,)
_ Is_delegate (type)
_ Is_empty (type)
_ Is_enum (type)
_ Is_interface_class (type)
_ Is_pod (type)
_ Is_polymorphic (type)
_ Is_ref_array (type)
_ Is_ref_class (type)
_ Is_sealed (type)
_ Is_simple_value_class (type)
_ Is_union (type)
_ Is_value_class (type) Special thanks to ctrlz @ newsmth for providing information. VC 2005 has been released for nearly two years and has not read all its extension syntax.