Read Loki:typelist feeling a

Source: Internet
Author: User


Problem one, simplified version of typelist, only support 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 two: You can get a value with 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);

Problem three, the type is no longer limited to int, but integrates a maximum of 3 variables.

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;

Read Loki:typelist feeling a

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.