-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP80.VariableTemplates.cpp
More file actions
84 lines (71 loc) · 2.21 KB
/
Copy pathP80.VariableTemplates.cpp
File metadata and controls
84 lines (71 loc) · 2.21 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
#include <iostream>
template<typename T>
class Foo
{
public:
static int var1;
inline static int var2; // inline so declaration is also definition, but can not initialize twice
inline static constexpr int var3 = 2;
template<typename U>
static int var4;
};
// definition of static data member of class template
template<typename T>
int Foo<T>::var1 = 10;
// sepcialization of var1
template<>
int Foo<int>::var1 = 99;
// var2 can be specilized, but can not have a redefinition
template<>
int Foo<int>::var2 = -1;
// definition of static template data member of class template
template<typename T>
template<typename U>
int Foo<T>::var4 = -1;
// definition of static template data member of specialization of class template
// this do not work for Foo<int>::var4<bool>, why?
template<>
template<typename U>
int Foo<int>::var4 = -2;
// full specialization
template<>
template<>
int Foo<int>::var4<int> = -3;
class Bar
{
public:
// declaration
template<typename T>
static int var;
static const int cvar; // do not initialize, need outter definition and initialization
static const int cvar2 = 1024; // initialize inside class, do not need a outter definition
static constexpr int cevar = -1; // only initialize inside
inline static int var2;
};
// definition
template<typename T>
int Bar::var = -1;
// specilization of template static data member of normal class
template<>
int Bar::var<bool> = 0;
// outter definition
const int Bar::cvar = -1;
int main(int argc, char const *argv[])
{
std::cout << Foo<bool>::var1 << " "
<< Foo<bool>::var2 << " "
<< Foo<bool>::var3 << std::endl;
std::cout << Foo<int>::var1 << " "
<< Foo<int>::var2 << " "
<< Foo<int>::var3 << std::endl;
std::cout << Foo<bool>::var4<int> << std::endl; // -1
std::cout << Foo<int>::var4<bool> << std::endl; // -1, why not -2?
std::cout << Foo<int>::var4<int> << std::endl; // -3
std::cout << Bar::var<int> << std::endl; // -10
std::cout << Bar::var<bool> << std::endl; // 0
std::cout << Bar::cvar << std::endl;
std::cout << Bar::cvar2 << std::endl;
std::cout << Bar::cevar << std::endl;
std::cout << Bar::var2 << std::endl;
return 0;
}