Read loki, TYPEList principle, lokitypelist

Source: Internet
Author: User

Read loki, TYPEList principle, lokitypelist

Read loki, principle of TYPEList
Question 1: the simplified version of TYPEList only supports int type
Template <int n>
Class TYPEList
{
Public:
Int Head;
TYPEList <n-1> Tail;

};

Template <>
Class TYPEList <0>
{
Public:
Int Tail;
Int Head;
};

Test code
TYPEList <0> l0;
L0.Head = 1;
L0.Tail = 2;

TYPEList <2> l2;
L2.Head = 3;
L2.Tail. Head = 4;
L2.Tail. Tail. Head = 5;
L2.Tail. Tail. Tail = 6;

Problem 2: The value can be obtained through GetAt (int ).
Template <int n>
Class TYPEList
{
Public:
Int Head;
TYPEList <n-1> Tail;
Int GetAt (int index)
{
If (n + 1 = index)
Return Head;
Return Tail. GetAt (index );
}
};

Template <>
Class TYPEList <0>
{
Public:
Int GetAt (int index)
{
If (1 = index)
Return Tail;
Return Head;
}
Int Tail;
Int Head;
};
Test code
TYPEList <0> l0;
L0.Head = 1;
L0.Tail = 2;

TYPEList <2> l2;
L2.Head = 3;
L2.Tail. Head = 4;
L2.Tail. Tail. Head = 5;
L2.Tail. Tail. Tail = 6;

Int i1 = l2.GetAt (0 );
Int i2 = l2.GetAt (1 );
Int i3 = l2.GetAt (2 );
Int i4 = l2.GetAt (3 );

Question 3: The type is no longer limited to int, but a maximum of three variables can be integrated.

Class NullType
{
};

Template <class T, class U>
Struct Typelist
{
T Head;
U Tail;
};

Template <typename T1 = NullType, typename T2 = NullType, typename T3 = NullType>
Struct MakeTypelist
{
Private:
Typedef typename MakeTypelist <T2, T3 >:: Result TailResult;
Public:
Typedef Typelist <T1, TailResult> Result ;};

Template <>
Struct MakeTypelist <>
{
Typedef NullType Result;
};

Test code:
MakeTypelist <ULONG, CString, int>: Result User, User2;

User. Head = 1;
User. Tail. Head = "He zhidan ";
User. Tail. Tail. Head = 35;

User2 = User;

 

Template <class AtomicType, template <class> class GenFunc>
Struct IterateTypes
{Template <class II>
Void operator () (II ii)
{
GenFunc <AtomicType> genfunc;
* Ii = genfunc ();
++ Ii; // Is this even needed?
}
};

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.