Typetraints of C + + generics

Source: Internet
Author: User

Complete code in

http://download.csdn.net/detail/zhuyingqingfen/8457091

#ifndef Typetraits_h_#define typetraits_h_//is only declared and not defined, it can only be used to denote "I am not an interesting type". Class nulltype;//This is a legitimate type that can be inherited, and you can pass Emptytype objects. Class EMPTYTYPE{};//1. Constant integer mapping to type/* generates different types based on different values. Generally, int2type1 can be used if the following conditions are met. It is necessary to call one or several different function 2 according to a compile-time constant.   It is necessary to implement "dispatch" (dispatch) at compile time (if else dispatch requires that each condition be compiled through, and Int2type does not produce a similar problem, because the compile time does not compile a template function that is not used, if a template The member function was never actually used, and C + + would not have it present. */template<int v>struct int2type{enum{value = v};};/ The only effect of/type2type is to eliminate the ambiguity (ambiguity) of overloaded functions. Class Template type2type// Converts each type to a unique, insipid type//invocation type2type<t> where T is a type//defines the type Origin Altype which maps back to T////////////////////////////////////////////////////////////////////////////////template <typename t>struct type2type{typedef T originaltype;};/ /type selection, judging by the first argument is the second parameter or the third parameter Template<bool flag,typename t,typename u>struct select{typedef T Result;}; Template<typenameT,typename u>struct select<false,t,u>{typedef U result;};/ /type traitstemplate <class t>class typetraints{private:template<class U>struct PointerTraints{enum{ result = False};typedef nulltype pointertype;}; Template<class u>struct Pointertraints<u*>{enum{result = true;}; typedef U PointerType;}; Template<class u>struct Ptomtraits{enum{result = false};}; Template<class u,class v>struct ptomtraits<u V::* >{enum{result = true};}; Public:enum{ispointer = Pointertraints<t>::result,ismemberpointer = Ptomtraits<t>::result};typedef TypeName Pointertraints<t>::P ointertype pointertype;}; #endif


Typetraints of C + + generics

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.