Is_pointer,is_member_function_pointer source analysis of C++11 type_traits

Source: Internet
Author: User

The source code is as follows:

 template<typename> struct   __is_  Pointer_helper:  public   False_type {}; Template  <typename _tp> struct  __is_pointer_helper<_tp*>< Span style= "color: #000000;"  >:  public   True_type {};  ///  Is_pointer  template< TypeName _tp> struct   Is_pointer:  
   
    public  integral_constant<
    bool , (__is_pointer_helper<
    typename REMOVE_CV  <_tp>::type>::value) >
     {}; 
   

First, you define two types, one true_type and one false_type, both of which inherit integral_constant. These two types are almost all is_xxx reused. And the standard library is also available for us to use.

Then, the template is biased, the version of the pointer type inherits True_type, and the non-pointer-type version inherits the False_type.

 1   ///integral_constant 2Template<typename _TP, _TP __v>3     structintegral_constant4     { 5       Staticconstexpr _TP value =__v;6typedef _TP VALUE_TYPE;7typedef INTEGRAL_CONSTANT&LT;_TP, __v>type;8constexproperatorValue_type () {returnvalue;} 9     };Ten    OneTemplate<typename _TP, _TP __v> Aconstexpr _tp INTEGRAL_CONSTANT&LT;_TP, __v>:: value; -  -   ///The type used as a compile-time Boolean with true value. thetypedef integral_constant<BOOL,true>True_type; -  -   ///The type used as a compile-time Boolean with false value. -typedef integral_constant<BOOL,false>False_type; +Template<typename> -     struct__is_member_function_pointer_helper +: PublicFalse_type {}; A  atTemplate<typename _tp, TypeName _cp> -     struct__IS_MEMBER_FUNCTION_POINTER_HELPER&LT;_TP _CP::* > -: Publicintegral_constant<BOOL, is_function<_tp>::value> { }; -  -   ///Is_member_function_pointer -Template<typename _tp> in     structIs_member_function_pointer -: Publicintegral_constant<BOOL, (__is_member_function_pointer_helper< toTypeName Remove_cv<_tp>::type>::value) > +{ };

Member pointers, which are slightly more complex, are similar to generic pointers, and the partial specificity of member pointers is written like this _tp _CP::*.

// Primary template.  /// Define a member typedef @c type to one of the argument types.  template<bool _cond, TypeName _iftrue, TypeName _iffalse>    struct  conditional    { typedef _iftrue type; };   // Partial specialization for false.  Template<typename _iftrue, TypeName _iffalse>    struct conditional<false, _iftrue , _iffalse>    {typedef _iffalse type;};

The conditional mechanism is similar to select in Loki, selects a type based on a Boolean value, and if _cond is true, selects the _iftrue type, otherwise selects another.

  /// is_reference  Template<typename _tp>    struct  is_reference    public __or_<is_lvalue_ Reference<_tp>,                   is_rvalue_reference<_Tp>>:: Type    {};

Is_reference is judged by or combined with lvalue references and rvalue references.

Template<typename...>struct__or_; Template<>struct__or_<>    :  PublicFalse_type {}; Template<typename _b1>struct__or_<_b1>    :  Public_B1 {}; Template<typename _b1, TypeName _b2>struct__OR_&LT;_B1, _b2>    :  PublicConditional<_b1::value, _B1, _b2>:: type {}; Template<typename _b1, TypeName _b2, TypeName _b3, TypeName ... _bn>struct__OR_&LT;_B1, _b2, _b3, _bn ... >    :  PublicConditional<_b1::value, _B1, __or_<_b2, _b3, _bn ... >>:: type {};

1.or inherits the type provided by conditional instead of the conditional itself.

2.conditional provides different types by the first type of Boolean in or, if the Boolean of B1 is True (also indicates that B1 inherits True_type), the remaining parameter or (recursive inheritance goes down) is not inherited.

3. Recursively, when there is a type left, the Boolean that directly inherits B1,B1 is the result of the whole or

4. Recursive to the end null argument, inherit False_type, the entire or result is false.

Is_pointer,is_member_function_pointer source analysis of C++11 type_traits

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.