-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathint2type5.cpp
52 lines (38 loc) · 1.05 KB
/
int2type5.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
/*
* HOME : ecourse.co.kr
* EMAIL : smkang @ codenuri.co.kr
* COURSENAME : C++ Template Programming
* MODULE : int2type5.cpp
* Copyright (C) 2017 CODENURI Inc. All rights reserved.
*/
#include <iostream>
#include <type_traits>
using namespace std;
template<typename T> struct IsPointer { static constexpr bool value = false; };
template<typename T> struct IsPointer<T*> { static constexpr bool value = true; };
// if 문을 사용한 함수 분기 : 실행시간 결정..
// 함수 오버로딩을 사용한 분기 : 컴파일 시간에 결정..
// 숫자로 함수 오버로딩하는 도구
template<int N> struct int2type
{
static const int value = N;
};
template<typename T> void printv_imp(T a, int2type<0>)
{
cout << a << endl;
}
template<typename T> void printv_imp(T a, int2type<1>)
{
cout << a << " : " << *a << endl;
}
template<typename T> void printv(T a)
{
// 함수 오버로딩은 컴파일 시간에 인자에 타입으로 함수 호출이 결정된다
printv_imp(a, int2type< IsPointer<T>::value >());
}
int main()
{
int n = 3;
printv(n);
printv(&n);
}