-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.cpp
113 lines (98 loc) · 2.09 KB
/
template.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/////////////////////////////////////////////////////////////////
// name : jppark
// 참조 : //http://blog.naver.com/blue7red?Redirect=Log&logNo=100042183231
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstddef>
template <typename T>
struct type_printer;
// 포인터형
template<typename T>
struct type_printer<T *>
{
std::ostream & print(std::ostream & out ) const
{
return out << type_printer<T>() << " *";
}
};
// 참조형
template<typename T>
struct type_printer<T &>
{
std::ostream & print(std::ostream & out ) const
{
return out << type_printer<T>() << " &";
}
};
// 배열
template<typename T, std::size_t N>
struct type_printer<T[N]>
{
std::ostream & print(std::ostream & out ) const
{
return out << type_printer<T>() << " [" << N << "]";
}
};
template<typename T>
struct type_printer<const T>
{
std::ostream & print(std::ostream & out ) const
{
return out << type_printer<T>() << " const";
}
};
template<typename T>
struct type_printer<volatile T>
{
std::ostream & print(std::ostream & out ) const
{
return out << type_printer<T>() << " volatile";
}
};
template<>
struct type_printer<int>
{
std::ostream & print(std::ostream & out ) const
{
return out << "int";
}
};
template<>
struct type_printer<char>
{
std::ostream & print(std::ostream & out ) const
{
return out << "char";
}
};
template<>
struct type_printer<long>
{
std::ostream & print(std::ostream & out ) const
{
return out << "long";
}
};
template<>
struct type_printer<short>
{
std::ostream & print(std::ostream & out ) const
{
return out << "short";
}
};
//출력
template<typename T>
std::ostream & operator<< (std::ostream & out, const type_printer<T> & desc )
{
return desc.print(out);
};
int main()
{
std:: cout << type_printer<int>() << "\n"
<< type_printer<char *>() << "\n"
<< type_printer<long const * &>() << "\n"
<< type_printer<short[10]>() << "\n"
<< type_printer<volatile int>() << "\n";
return 0;
}