-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcouple.cpp
81 lines (61 loc) · 1.54 KB
/
couple.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
71
72
73
74
75
76
77
78
79
80
81
/*
* HOME : ecourse.co.kr
* EMAIL : smkang @ codenuri.co.kr
* COURSENAME : C++ Template Programming
* MODULE : couple.cpp
* Copyright (C) 2017 CODENURI Inc. All rights reserved.
*/
#include <iostream>
using namespace std;
template<typename T> void printN(const T& a) { cout << T::N << endl; }
// 임의의 타입 2개를 보관하는 구조체
template<typename T, typename U> struct couple
{
T v1;
U v2;
static constexpr int N = 2;
};
// 2번째 인자가 recursive일때를 위한 부분전문화
template<typename A, typename B, typename C> struct couple<A, couple<B, C>>
{
A v1;
couple<B, C> v2;
static constexpr int N = couple<B, C>::N + 1; // 핵심!
};
template<typename A, typename B, typename C> struct couple<couple<A, B>, C>
{
couple<A, B> v1;
C v2;
static constexpr int N = couple<A, B>::N + 1; // 핵심!
};
template<typename A, typename B, typename C, typename D> struct couple<couple<A, B>, couple<C, D>>
{
couple<A, B> v1;
couple<C, D> v2;
static constexpr int N = couple<A, B>::N + couple<C, D>::N; // 핵심!
};
int main()
{
couple<couple<int, int>, couple<int, int>> d4;
printN(d4); // 4나와야 합니다.
}
/*
int main()
{
couple<couple<int, int>, int> d3;
couple<couple<couple<int, int>, int>, int> d4;
printN(d3); // 3
printN(d4); // 4 나오게 해보세요
}
*/
/*
int main()
{
couple<int, double> d2;
couple<int, couple<int, int>> d3;
couple<int, couple<int, couple<int, int>>> d4;
printN(d2); // 2
printN(d3); // 3
printN(d4); // 4
}
*/