-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTypelist5_TypeAt.cpp
64 lines (44 loc) · 1.35 KB
/
Typelist5_TypeAt.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
#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의 N 번째 요소의 타입 구하기
// 1. 사용하는 코드를 보고 primary template 작성.
// T : Typelist
template<typename T, int N> struct TypeAt;
// 2. 원하는 타입을 구할수 있도록 부분특수화
// T : Typelist의 요소 타입
/*
template<typename T, typename U, int N> struct TypeAt<Typelist<T, U>, N>
{
typedef ? type;
};
*/
// N == 0 일때.
template<typename T, typename U> struct TypeAt<Typelist<T, U>, 0>
{
typedef T type;
};
// N != 0 일때.
template<typename T, typename U, int N> struct TypeAt<Typelist<T, U>, N>
{
typedef typename TypeAt<U, N-1>::type type;
};
template<typename T> void test()
{
typename TypeAt<T, 3>::type n; // char
cout << typeid(n).name() << endl;
}
int main()
{
test<TYPELIST_4(int, char, double, long)>();
}