-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP344.FullMemberSpecialization.cpp
More file actions
136 lines (126 loc) · 2.96 KB
/
Copy pathP344.FullMemberSpecialization.cpp
File metadata and controls
136 lines (126 loc) · 2.96 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <string>
template<typename T>
class X
{
public:
// member function template
template<typename U>
void tf()
{
std::cout << "X<T>::tf<U>()" << std::endl;
}
// static member function template
template<typename U>
static void stf()
{
std::cout << "X<T>::stf<U>()" << std::endl;
}
// static data member template
template<typename U>
inline static std::string ststr = "X<T>::ststr<U>()";
// ordinary member function
void f()
{
std::cout << "X<T>::f()" << std::endl;
}
// ordinary static data member
static std::string sstr; // = "X<T>::sstr";
// ordinary static member function
static void sf()
{
std::cout << "X<T>::sf()" << std::endl;
}
};
template<typename T>
inline std::string X<T>::sstr = "X<T>::sstr"; // defining as inline inside class will cause reinitialization
// full specialization of member template
template<>
template<>
void X<int>::tf<int>()
{
std::cout << "X<int>::tf<int>()" << std::endl;
}
template<>
template<>
void X<int>::stf<int>()
{
std::cout << "X<int>::stf<int>()" << std::endl;
}
template<>
template<>
inline std::string X<int>::ststr<int> = "X<int>::ststr<int>";
// full specialization of ordinary member
template<>
void X<int>::f()
{
std::cout << "X<int>::f()" << std::endl;
}
template<>
inline std::string X<int>::sstr = "X<int>::sstr";
template<>
void X<int>::sf()
{
std::cout << "X<int>::sf()" << std::endl;
}
// specialization of member template of ordinary class
class Y
{
public:
// member function template
template<typename U>
void tf()
{
std::cout << "Y::tf<U>()" << std::endl;
}
// static member function template
template<typename U>
static void stf()
{
std::cout << "Y::stf<U>()" << std::endl;
}
// static data member template
template<typename U>
inline static std::string ststr = "Y::ststr<U>()";
};
// full specialization of member template of ordinary class
template<>
void Y::tf<int>()
{
std::cout << "Y::tf<int>()" << std::endl;
}
template<>
void Y::stf<int>()
{
std::cout << "Y::stf<int>()" << std::endl;
}
template<>
inline std::string Y::ststr<int> = "Y::ststr<int>";
int main(int argc, char const *argv[])
{
// generic version
X<bool>().tf<bool>();
X<bool>::stf<bool>();
std::cout << X<bool>::ststr<bool> << std::endl;
X<bool>().f();
X<bool>::sf();
std::cout << X<bool>::sstr << std::endl;
// full specialization version
X<int>().tf<int>();
X<int>::stf<int>();
std::cout << X<int>::ststr<int> << std::endl;
X<int>().f();
X<int>::sf();
std::cout << X<int>::sstr << std::endl;
std::cout << std::endl;
// ordinary class
// generic version
Y().tf<bool>();
Y::stf<bool>();
std::cout << Y::ststr<bool> << std::endl;
// full sepcialization version
Y().tf<int>();
Y::stf<int>();
std::cout << Y::ststr<int> << std::endl;
return 0;
}