-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathspecialization2.cpp
More file actions
74 lines (48 loc) · 1.33 KB
/
specialization2.cpp
File metadata and controls
74 lines (48 loc) · 1.33 KB
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
/*
* HOME : ecourse.co.kr
* EMAIL : smkang @ codenuri.co.kr
* COURSENAME : C++ Template Programming
* MODULE : specialization2.cpp
* Copyright (C) 2017 CODENURI Inc. All rights reserved.
*/
#include <iostream>
using namespace std;
template<typename T, typename U> struct Test
{
static void foo() { cout << "T, U" << endl; }
};
template<typename T, typename U> struct Test<T*, U>
{
static void foo() { cout << "T*, U" << endl; }
};
template<typename T, typename U> struct Test<T*, U*>
{
static void foo() { cout << "T*, U*" << endl; }
};
// 핵심 : 부분 특수화 시에 템플릿 인자의 갯수는 변할수 있다
template<typename T> struct Test<T, T>
{
static void foo() { cout << "T, T" << endl; }
};
template<typename U> struct Test<int, U>
{
static void foo() { cout << "int, U" << endl; }
};
// int, int : 특수화..
template<> struct Test<int, int>
{
static void foo() { cout << "int, int" << endl; }
};
template<> struct Test<int, short>
{
static void foo() { cout << "int, short" << endl; }
};
int main()
{
Test<int, double>::foo(); // T, U
Test<int*, double>::foo(); // T*, U
Test<int*, double*>::foo(); // T*, U*
Test<int, int>::foo(); // T, T => int, int
Test<int, char>::foo(); // int, U
Test<int, short>::foo(); // int, short
}