-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathenable_if2.cpp
64 lines (50 loc) · 1.13 KB
/
enable_if2.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
/*
* HOME : ecourse.co.kr
* EMAIL : smkang @ codenuri.co.kr
* COURSENAME : C++ Template Programming
* MODULE : enable_if2.cpp
* Copyright (C) 2017 CODENURI Inc. All rights reserved.
*/
#include <iostream>
#include <type_traits>
using namespace std;
// 포인터 일때와 포인터가 아닐때
/*
template<typename T> void printv(T a)
{
// 방법 1. if 문 사용..
if (is_pointer<T>::value)
{
//....
}
else
{
//....
// *a = 0; error...
}
}
*/
// 방법 2. true_type, false_type 으로 오버로딩..
/*
template<typename T> void printv_imp(T a, true_type) {}
template<typename T> void printv_imp(T a, false_type) {}
template<typename T> void printv(T a)
{
printv_imp(a, is_pointer<T>());
}
*/
// 방법 3. enable_if 사용.
template<typename T> typename enable_if< is_pointer<T>::value, void>::type printv(T a)
{
cout << "포인터 일때" << endl;
}
template<typename T> typename enable_if< !is_pointer<T>::value, void>::type printv(T a)
{
cout << "포인터 아닐때" << endl;
}
int main()
{
int n = 10;
printv(n);
printv(&n);
}