forked from facebook/fatal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.1-values.cpp
279 lines (251 loc) · 7.14 KB
/
1.1-values.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <fatal/lesson/driver.h>
namespace lesson {
/**
* @author: Marcelo Juchem <[email protected]>
*/
LESSON(
"representing values, part 1/4",
"This lesson gives an overview on how values are represented. Subsequent "
"tutorials will elaborate on proper ways of achieving such representation."
"\n\n"
"The goal, for now, is to come up with the intuition behind it without "
"drowing in syntax and correctness.",
template <int Value>
struct int_constant {
static int value;
};
template <int Value>
int int_constant<Value>::value = Value;
) {
COMMENT(
"A previous lesson mentioned that values can be emulated using types to "
"represent them. Here's an overview on the intuition of how this can be "
"achieved."
);
CODE(
using x = int_constant<15>;
);
TYPE(x);
VALUE(x::value);
COMMENT(
"Note, however, that `int_constant::value` is a regular runtime variable "
"as opposed to a compile time constant. It is possible, for instance, to "
"change the value associated with it:"
);
CODE(
x::value = 30;
);
VALUE(x::value);
COMMENT(
"This makes it illegal to use such variable as an argument to a template. "
"Template parameters must be immutable and available at compile time. This "
"includes, for instance, type and integer constants."
);
ILLEGAL(
"`int_constant::value` is not a constant",
using y = int_constant<x::value>;
);
}
/**
* @author: Marcelo Juchem <[email protected]>
*/
LESSON(
"representing values, part 2/4",
"This lesson demonstrates proper ways to represent values that can be used "
"at compile time."
"\n\n"
"Let's modify the `int_constant` template to properly represent compile time "
"constants.",
template <int Value>
struct int_constant_proper {
static constexpr int const value = Value;
};
template <int Value>
constexpr int const int_constant_proper<Value>::value;
) {
COMMENT(
"The `constexpr` keyword roughly allows us to tell the compiler that a "
"given variable holds the result of a constant expression."
"\n\n"
"Once we have such guarantee, the compiler can evaluate the contents of "
"such variable at compile time, effectivelly making it a compile time "
"constant."
);
CODE(
using x = int_constant_proper<15>;
);
TYPE(x);
VALUE(x::value);
COMMENT(
"As noted before, constants can be used as template parameters."
);
CODE(
using y = int_constant_proper<x::value>;
);
TYPE(y);
VALUE(y::value);
COMMENT(
"In fact, any expression that can be evaluated at compile time can be used "
"as a compile time constant:"
);
CODE(
using z = int_constant_proper<x::value * 2>;
);
TYPE(z);
VALUE(z::value);
CODE(
using w = int_constant_proper<x::value + z::value - 3>;
);
TYPE(w);
VALUE(w::value);
}
/**
* @author: Marcelo Juchem <[email protected]>
*/
LESSON(
"representing values, part 3/4",
"This lesson gives an overview on the implementation of "
"`std::integral_constant`."
"\n\n"
"So far we've been limited to `int` constants. One could be interested in "
"employing other types for a constant, like `char` or `unsigned long`."
"\n\n"
"Let's modify the `int_constant_proper` template to represent arbitrary "
"integral types.",
template <typename T, T Value>
struct constant {
static constexpr T const value = Value;
};
template <typename T, T Value>
constexpr T const constant<T, Value>::value;
) {
COMMENT(
"Now we can specify the type of the constant, as well as its value."
);
CODE(
using x = constant<int, -15>;
);
TYPE(x);
VALUE(x::value);
CODE(
using y = constant<bool, true>;
);
TYPE(y);
VALUE(y::value);
COMMENT(
"Again, any expression that can be evaluated at compile time will do:"
);
CODE(
using z = constant<unsigned, (x::value > 0) ? x::value : -x::value>;
);
TYPE(z);
VALUE(z::value);
}
/**
* @author: Marcelo Juchem <[email protected]>
*/
LESSON(
"representing values, part 4/4",
"This lesson gives an overview of some basic features that "
"`std::integral_constant` offers."
"\n\n"
"The implementation and library features built around "
"`std::integral_constant` are a bit more involved than what we've seen so "
"far, but for the purposes of a lesson, we don't need to dig too deep."
"\n\n"
"For now, let's look at a few more things that `std::integral_constant` "
"offers."
) {
COMMENT(
"We already covered how to represent a compile time constant with a type, "
"and how to access the constant's value."
);
CODE(
using x = std::integral_constant<int, -15>;
);
TYPE(x);
VALUE(x::value);
COMMENT(
"For convenience purposes, `std::integral_constant` also provides an "
"identity alias in the form of a member called `type`:"
);
TYPE(x::type);
COMMENT(
"It also exposes the type of the constant it represents:"
);
TYPE(x::value_type);
COMMENT(
"Shortcuts to boolean constants are also provided:"
);
CODE(
using t = std::true_type;
);
TYPE(t);
VALUE(t::value);
TYPE(t::value_type);
CODE(
using f = std::false_type;
);
TYPE(f);
VALUE(f::value);
TYPE(f::value_type);
}
/**
* @author: Marcelo Juchem <[email protected]>
*/
LESSON(
"convenience aliases",
"This lesson gives an overview on how to reduce verbosity through the use of "
"convenience aliases."
"\n\n"
"Some types will be extensively used throughout the examples in this lesson. "
"For instance, `std::integral_constant` for `int` values."
"\n\n"
"For this reason, let's see how we can shorten the code we write when "
"declaring an integral constant through the use of aliases.",
template <int Value>
using int_value = std::integral_constant<int, Value>;
) {
COMMENT(
"Let's start by going the verbose route and fully specifying `x` as an "
"`std::integral_constant`."
);
CODE(
using x = std::integral_constant<int, 10>;
);
TYPE(x);
VALUE(x::value);
COMMENT(
"Now let's use the convenient alias `int_value` to declare the same thing."
);
CODE(
using y = int_value<10>;
);
TYPE(y);
VALUE(y::value);
COMMENT(
"The beauty of aliases is that they don't create new types. Instead, "
"they're just shortcuts to existing types. For instance, by checking the "
"output of this lesson, it's easy to see that both `x` and `y` reference "
"exactly the same type: `std::integral_constant<int, 10>`."
"\n\n"
"The code below will be further explained in a later lesson. For now, it "
"suffices to know that it will prevent the program from compiling "
"if both `x` and `y` do not represent the same type."
"\n\n"
"This means that, if the line below doesn't result in a compilation error, "
"then both `x` and `y` are guaranteed to reference the same type."
);
CODE(
static_assert(std::is_same<x, y>::value, "type mismatch");
);
}
} // namespace lesson {