-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTypelist6_Append.cpp
70 lines (51 loc) · 1.88 KB
/
Typelist6_Append.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
template<typename T, typename U> struct Typelist
{
typedef T Head;
typedef U Tail;
};
struct NullType {};
#define TYPELIST_1(T1) Typelist<T1, NullType>
#define TYPELIST_2(T1, T2) Typelist<T1, Typelist<T2, NullType>>
#define TYPELIST_3(T1, T2, T3) Typelist<T1, Typelist<T2, Typelist<T3, NullType>>>
#define TYPELIST_4(T1, T2, T3, T4) Typelist<T1, Typelist<T2, Typelist<T3, Typelist<T4, NullType>>>>
//-------------------------------------------------------------------------------------------
// Typelist 끝에 타입 추가하기.
template<typename TL, typename T> struct Append;
// TL T
// 1. NullType, NullType => NullType
template<> struct Append<NullType, NullType>
{
typedef NullType type;
};
// 2. NullType, 임의의타입 => Typelist<임의의타입, NullType>
template<typename T> struct Append<NullType, T>
{
typedef Typelist<T, NullType> type;
};
// 3. NullType, Typelist<Head, Tail> => Typelist<Head, Tail>
template<typename Head, typename Tail> struct Append<NullType, Typelist<Head, Tail> >
{
typedef Typelist<Head, Tail> type;
};
// 4. Typelist<Head, Tail>, NullType => Typelist<Head, Tail>
// 이번 단계의 코드는 없어도 됩니다. 5단계의 코드만 있으면 됩니다.
template<typename Head, typename Tail> struct Append<Typelist<Head, Tail>, NullType >
{
typedef Typelist<Head, Tail> type;
};
// 5. Typelist<Head, Tail>, T => Typelist<Head, Append<Tail, T>::type>
template<typename Head, typename Tail, typename T> struct Append<Typelist<Head, Tail>, T >
{
typedef Typelist<Head, typename Append<Tail, T>::type> type;
};
template<typename T> void test()
{
typename Append<T, int>::type t1;
cout << typeid(t1).name() << endl; // int, char, double, int, NullType
}
int main()
{
test<TYPELIST_3(int, char, double)>();
}