-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp14_variable_template.cpp
More file actions
68 lines (53 loc) · 1.52 KB
/
cpp14_variable_template.cpp
File metadata and controls
68 lines (53 loc) · 1.52 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
#include <stdio.h>
template <typename T>
constexpr T pi = T(3.123456789);
void test_1() {
int iPi = pi<int>;
float fPi = pi<float>;
double dPi = pi<double>;
printf("%d\n", iPi);
printf("%f\n", fPi);
printf("%E\n", dPi);
}
/////////////////////////////////////////////////////////////////////////////////
// Function template using variable template -
// From https://en.cppreference.com/w/cpp/language/variable_template
/////////////////////////////////////////////////////////////////////////////////
// Function template
template<class T>
T get2Pi(T r)
{
return pi<T> * 2; // pi<T> is a variable template instantiation
}
void test_2() {
int iPi;
float fPi;
double dPi;
iPi = get2Pi(iPi);
fPi = get2Pi(fPi);
dPi = get2Pi(dPi);
printf("%d\n", iPi);
printf("%f\n", fPi);
printf("%E\n", dPi);
}
/////////////////////////////////////////////////////////////////////////////////
// Additional from Pluralsight - Practical C++14/C++17 by Giovanni Dicanio
/////////////////////////////////////////////////////////////////////////////////
template <typename T>
constexpr T maxValue = T(1000);
// Template specializations
template <>
constexpr double maxValue<double> = 2000;
template <>
constexpr char maxValue<char> = 'Z';
void test_3() {
printf(" maxValue<int> = %d\n", maxValue<int>);
printf(" maxValue<double> = %f\n", maxValue<double>);
printf(" maxValue<char> = %c\n", maxValue<char>);
}
int main() {
test_1();
test_2();
test_3();
return 0;
}