-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdecltype.cpp
More file actions
177 lines (140 loc) · 4.55 KB
/
decltype.cpp
File metadata and controls
177 lines (140 loc) · 4.55 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// =====================================================================================
// Decltype.cpp // decltype and std::declval
// =====================================================================================
module modern_cpp:decltype_keyword;
namespace Decltype {
// decltype may modify type deduction, e.g. in generic lambdas
// (decltype (t + u), decltype (t) or decltype (u) are valid)
template <typename T, typename U>
auto add(const T& t, const U& u) -> decltype (t + u)
{
return t + u;
}
// or without trailing return type:
template <typename T, typename U>
decltype (std::declval<T>() + std::declval<U>())
add2(const T& t, const U& u)
{
return t + u;
}
static void test_01()
{
int n{ 1 };
double d{ 2.7 };
auto result = add(n, d);
std::cout << result << std::endl;
}
static void test_02()
{
/* decltype in combination with metaprogramming techiques
*/
std::vector<int> vec{ 1 };
// yiedling a lvalue reference
vec[0] = 123;
std::vector<int>::value_type value = 123;
// doesn't compile !!! type is int&, not int
// decltype (vec[0]) anotherValue = 123;
// int&
decltype (vec[0]) anotherValue = value;
// retrieve value type from vector
using ValueType = std::remove_reference <decltype (vec[0])>::type;
ValueType yetAnotherValue = 123;
// same as:
using AnotherValueType = std::remove_reference<int&>::type;
AnotherValueType oneMoreValue = 123;
// using std::vector's reference type
std::vector<int>::reference refWert = value;
// doesn't compile !!! type is int&, not int
// std::vector<int>::reference refWert = 123;
}
// demonstrating decltype with entities / instances of types:
static void test_03()
{
std::vector<int> vec;
// decltype(vec) yields std::vector<int>,
// so the next line is equivalent to 'std::vector<int> vec2;'
decltype(vec) vec2;
// foo returns the type of f, in this case float,
// so this is equivalent to float foo(int b);
float f{};
auto foo(int b) -> decltype(f);
}
// demonstrating decltype with expressions:
static void test_04()
{
// decltype(foo()) yields the type of
// whatever foo() returns, in this case: float:
float foo();
decltype(foo()) b = decltype(foo()){};
//decltype yields void, so this is the same as void bar();
std::vector<int> vec;
auto bar() -> decltype(vec.push_back(int{}));
}
template<typename T, typename U>
using sum_t = decltype(std::declval<T>() + std::declval<U>());
template<typename T, typename U>
sum_t<T, U> summe(T a, U b)
{
return a + b;
}
static void test_05()
{
sum_t<int, float> result{ summe(123, 123.99F) };
std::cout << result << std::endl;
}
}
namespace Decltype_Auto_Vs_Templates {
// a) using auto, no trailing return type deduction
static auto mimimum1(auto n, auto m) // -> decltype (n + m)
{
decltype (n + m) result;
result = (n < m) ? n : m;
return result;
}
// b) using auto and trailing return type deduction
static auto mimimum2(auto n, auto m) -> decltype (n + m)
{
if (n < m)
return n;
else
return m;
}
template <typename T, typename U>
static auto mimimum3(T n, U m) -> decltype (n + m)
{
if (n < m)
return n;
else
return m;
}
// c) *Not* using auto and *not* using trailing return type deduction
// demonstrating how return data type can be 'computed'
template <typename T, typename U>
decltype (std::declval<T>() + std::declval<U>()) static mimimum4(T n, U m)
{
if (n < m)
return n;
else
return m;
}
static void test_06() {
auto result1 = mimimum1(100.0, 200l);
auto result2 = mimimum2(100.0, 200l);
auto result3 = mimimum3(100.0, 200l);
auto result4 = mimimum4(100.0, 200l);
}
}
void main_decltype()
{
using namespace Decltype;
test_01();
test_02();
test_03();
test_04();
test_05();
using namespace Decltype_Auto_Vs_Templates;
test_06();
}
// =====================================================================================
// End-of-File
// =====================================================================================