-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTypelist4_Length.cpp
52 lines (36 loc) · 1.11 KB
/
Typelist4_Length.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
#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의 요소 갯수 구하기.
// 1. 사용하는 모습을 보고 primary template 작성.
template<typename T> struct Length;
//{
//};
// 2. 갯수를 구할수 있도록 부분 특수화
template<typename T, typename U> struct Length<Typelist<T, U>>
{
enum { value = Length<U>::value + 1 };
};
// 3. 재귀를 종료 하기 위한 전문화(특수화)
template<> struct Length<NullType>
{
enum { value = 0 };
};
template<typename T> void test()
{
cout << Length<T>::value << endl; // 4
}
int main()
{
test< TYPELIST_4(int, char, short, double) >();
}