Skip to content

Commit d493937

Browse files
committed
ch03: Migration to consistent styling and C++17 for first part of ch03
1 parent 06676fd commit d493937

File tree

3 files changed

+82
-89
lines changed

3 files changed

+82
-89
lines changed
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
# Comments
22

3-
```admonish warning
4-
🚧 Page Under Construction! 🏗️
5-
```
6-
73
Comments are a way to document code for other people, and yourself. In C++ there are two
84
types of comments, single line and multi-line. We've seen single line comments in many of
95
the previous examples but to reiterate, a single line comment is started with `//` and
106
any text written after it until a newline is ignored by the compiler.
117

12-
```cpp
8+
```cpp,icon=%cplusplus
139
// Comment on its own line
1410
15-
auto const x = 5; // Comment
11+
const auto x = 5; // Comment
1612
```
1713

1814
Multi-line comments are specified using `/* */` *quoting* ie. the comment extends from
1915
`/*` comment opener and continues until `*/`. This allows comments to extend multiple
2016
lines or be nested amongst code (if you really want).
2117

22-
```cpp
18+
```cpp,icon=%cplusplus
2319
/*
2420
multi-line comment
2521
another line
2622
*/
2723
28-
auto const /* int */ x = 5;
24+
const auto /* int */ x = 5;
2925
```
3026

src/ch03-common-concepts/data-types.md

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# Data Types
22

3-
```admonish warning
4-
🚧 Page Under Construction! 🏗️
5-
```
6-
73
As we mentioned on the last page, C++ is a *statically typed* language which means the
84
type of data must be known (or deducable) to the compiler. C++ has a large selection of
95
types available to use, some are language primitives and others are defined in the
@@ -22,9 +18,9 @@ diffenent bit widths. The default `int` is 32-bits wide on most platforms. By de
2218
integer types are signed ie. they can represent both positive and negative numbers. If
2319
you need unsigned numbers we can use the `unsigned` qualifier.
2420

25-
```cpp
26-
int const x = -5;
27-
unsigned int const y = 5;
21+
```cpp,icon=%cplusplus
22+
const int x = -5;
23+
const unsigned int y = 5;
2824
```
2925

3026
If you need integers of a different sizes you can either use size qualifiers with the
@@ -110,18 +106,18 @@ with the `u` suffix being able to be used in combination with the other two.
110106
Additionally you can write integer literals in a different base form by changing the
111107
prefix of the literal.
112108

113-
```cpp
114-
auto const decimal = 42;
115-
auto const octal = 052;
116-
auto const hex = 0x2a;
117-
auto const Hex = 0X2A; // capital hex digits
118-
auto const binary 0b101010;
109+
```cpp,icon=%cplusplus
110+
const auto decimal = 42;
111+
const auto octal = 052;
112+
const auto hex = 0x2a;
113+
const auto Hex = 0X2A; // capital hex digits
114+
const auto binary 0b101010;
119115
```
120116

121117
Integers can also be separated using a `'` to make large numbers easier to read.
122118

123-
```cpp
124-
auto const x = 1'234'567'890;
119+
```cpp,icon=%cplusplus
120+
const auto x = 1'234'567'890;
125121
```
126122

127123
### Character Types
@@ -131,17 +127,17 @@ because character types in C++ are represented using numbers, specifically `char
131127
represents ASCII code points. Character literals are specified with single quotes like
132128
the example below.
133129

134-
```cpp
135-
char const x = 'a';
136-
auto const y = 'b';
130+
```cpp,icon=%cplusplus
131+
const char x = 'a';
132+
const auto y = 'b';
137133
```
138134

139135
### Boolean Type
140136

141137
C++'s Boolean type is called `bool` and can either hold the value `true` or `false`.
142138
Booleans are used mostly in conditional and loop statements eg. `if` and `while`.
143139

144-
```cpp
140+
```cpp,icon=%cplusplus
145141
bool x = false;
146142
auto y = true;
147143
```
@@ -164,41 +160,42 @@ floating point number.
164160
With `auto`, floating point values being initialized as a `double` by default and `float`
165161
and `long double` literals being specified by `f` and `l` literal suffixes.
166162

167-
```cpp
168-
auto const f = -0.06f;
169-
auto const d = 47.5768;
170-
auto const l = -655456.457567l;
163+
```cpp,icon=%cplusplus
164+
const auto f = -0.06f;
165+
const auto d = 47.5768;
166+
const auto l = -655456.457567l;
171167
```
172168

173169
We can also initialize floating points using exponential form:
174170

175-
```cpp
176-
auto const f = -6e-2f;
177-
auto const d = 475768e4;
178-
auto const l = -655456457567le7l;
171+
```cpp,icon=%cplusplus
172+
const auto f = -6e-2f;
173+
const auto d = 475768e4;
174+
const auto l = -655456457567le7l;
179175
```
180176

181177
#### Arithmetic Operations
182178

183179
Integral and floating point types are categorized as *arithmetic* types which mean they
184180
support the common arithmetic operations like addition, subtraction etc.
185-
```cpp
181+
182+
```cpp,icon=%cplusplus,fp=main.cxx
186183
auto main() -> int {
187184
// addition
188-
auto const sum = 4 + 6;
185+
const auto sum = 4 + 6;
189186

190187
// subtraction
191-
auto const diff = 10 - 5.5;
188+
const auto diff = 10 - 5.5;
192189

193190
// multiplication
194-
auto const mul = 5 * 3.2;
191+
const auto mul = 5 * 3.2;
195192

196193
// division
197-
auto const idiv = 10 / 3;
198-
auto const fdif = 13.5 / 2.4;
194+
const auto idiv = 10 / 3;
195+
const auto fdif = 13.5 / 2.4;
199196

200197
// remainder
201-
auto const = 23 % 4;
198+
const auto = 23 % 4;
202199

203200
return 0;
204201
}
@@ -223,14 +220,14 @@ restricted to a set of named variants or *enumerators*. These named constants ha
223220
underlying integral type. Specifying the underlying type is optional ie. omit the
224221
`: type` in the enum declaration.
225222

226-
```cpp
223+
```cpp,icon=%cplusplus
227224
enum class colour : char {
228225
red,
229226
green,
230227
blue
231228
};
232229
233-
auto const c = colour::red;
230+
const auto c = colour::red;
234231
```
235232

236233

@@ -242,33 +239,33 @@ declared. Tuples in C++ are not language types but are provided by the standard
242239
in the `<tuple>` header and is called `std::tuple`. We create a tuple using brace
243240
initialization (top) or using the helper function `std::make_tuple()`.
244241

245-
```cpp
246-
auto const t = std::tuple { 5u, 5.34f, -345, "abc", false };
247-
auto const u = std::make_tuple(5u, 5.f, -345, "abc", false);
242+
```cpp,icon=%cplusplus
243+
const auto t = std::tuple { 5u, 5.34f, -345, "abc", false };
244+
const auto u = std::make_tuple(5u, 5.f, -345, "abc", false);
248245
```
249246

250247
Tuples can be accessed using `std::get<I>(t)` with `I` being the index of the value we
251248
want to access and `t` is the tuple object.
252249

253-
```cpp
254-
auto const e = std::get<2>(t); // e := -345
250+
```cpp,icon=%cplusplus
251+
const auto e = std::get<2>(t); // e := -345
255252
```
256253

257254
You can also destructure tuples into its constituent values like so.
258255

259-
```cpp
260-
auto const [v, w, x, y, z] = t;
256+
```cpp,icon=%cplusplus
257+
const auto [v, w, x, y, z] = t;
261258
```
262259

263260
There is a specialization of *tuples* called `std::pair` which holds just two values. The
264261
values of a pair can be extracted using the same methods as tuples but they also have
265262
public members `std::pair::first` and `std::pair::second` which allows you to access the
266263
data.
267264

268-
```cpp
269-
auto const p = std::pair {5, 'a'};
270-
auto const [x, y] = p;
271-
auto const z = p.second;
265+
```cpp,icon=%cplusplus
266+
const auto p = std::pair {5, 'a'};
267+
const auto [x, y] = p;
268+
const auto z = p.second;
272269
```
273270

274271
## Special Types
@@ -279,8 +276,8 @@ fundamental to the language.
279276
The first is the `void` type is an incomplete type that is used to indicate that a
280277
function does not return a value.
281278

282-
```cpp
283-
auto foo(auto const i) -> void {
279+
```cpp,icon=%cplusplus
280+
auto foo(const auto i) -> void {
284281
i + 5;
285282
}
286283
```
@@ -297,9 +294,9 @@ with indices starting at 0. The subscript element access does not perform bounds
297294
while `array::at()` does, meaning the later will throw and exception if an out of bounds
298295
index is used while the former will crash the program... sometimes.
299296

300-
```cpp
301-
auto const a = std::array { 1, 2, 3, 4, 5 };
302-
auto const e1 = a[0]; // valid
303-
auto const e2 = a.at(5); // exception std::out_of_range
297+
```cpp,icon=%cplusplus
298+
const auto a = std::array { 1, 2, 3, 4, 5 };
299+
const auto e1 = a[0]; // valid
300+
const auto e2 = a.at(5); // exception std::out_of_range
304301
```
305302

src/ch03-common-concepts/vars-mut.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# Variables and Mutability
22

3-
```admonish warning
4-
🚧 Page Under Construction! 🏗️
5-
```
6-
73
We first saw variables in our mini [guessing game project](/ch02-guessing-game/guessing-game.md#storing-data-with-variables)
8-
where we used them to store the guess of the user and create our PRNG etc.. Let's exlore
4+
where we used them to store the guess of the user and create our PRNG etc.. Let's explore
95
what happens when we try to modify constant data and when we would want to allow
106
mutations.
117

@@ -16,31 +12,37 @@ the `const` keyword which data that does not need to change, cannot change; opti
1612
remove the `const` keyword when data needs to be mutable.
1713
```
1814

19-
Create a new (or use an existing project) with a `main.cxx` and `CMakeLists.txt` etc.
20-
like we did for our previous programs; or use an existing one, and we'll explore
21-
mutability. Change the name of the target to `main` in the CMakeLists.txt, as I'll be
22-
using this as the target name from (near) all examples from now on in the book.
15+
Create a new project have done before, with a `main.cxx` and `CMakeLists.txt` and add the
16+
following contents. This will act as out scratchbook project for tinkering with examples.
17+
I won't always go into super detail about what changes will be made between various
18+
topics but most examples will have a full example with some being hidden behind snips
19+
which can be exposed using the 'eye' button in a codeblock.
2320

24-
```haskell
21+
```haskell,icon=%cmake,fp=CMakeLists.txt
2522
cmake_minimum_required(VERSION 3.22)
2623
2724
project(main
2825
VERSION 0.1.0
29-
DESCRIPTION "C++ Book Example"
26+
DESCRIPTION "C++ Book Examples"
3027
LANGUAGES CXX)
3128
3229
add_executable(main main.cxx)
33-
target_compile_features(main PRIVATE cxx_std_20)
30+
target_compile_features(main PRIVATE cxx_std_17)
31+
32+
if (MSVC)
33+
# warning level 4
34+
add_compile_options(/W4)
35+
else()
36+
# additional warnings
37+
add_compile_options(-Wall -Wextra -Wpedantic)
38+
endif()
3439
```
3540

36-
In your `main.cxx`, write the following program. When we try to compile this we should
37-
get an error like so.
38-
39-
```cpp
41+
```cpp,icon=%cplusplus,fp=main.cxx
4042
#include <iostream>
4143

4244
auto main() -> int {
43-
auto const x = 42;
45+
const auto x = 42;
4446

4547
std::cout << x << std::endl;
4648
x = 43;
@@ -52,8 +54,8 @@ auto main() -> int {
5254

5355
When we try to compile this we should get an error like so:
5456

55-
```sh
56-
$ cmake -S . -B build --preset=<platform>
57+
```sh,icon=%gnubash,fp=Shell
58+
$ cmake -S . -B build
5759
$ cmake --build build
5860
[ 50%] Building CXX object CMakeFiles/main.dir/main.cxx.o
5961
/home/user/projects/common/main.cxx: In function ‘int main()’:
@@ -73,10 +75,10 @@ function boundaries where we expect the function to not mutate data passed to it
7375
though the surrounding scope might. More on this later.
7476

7577
Even though immutable data is easier to reason about, mutating data is where the fun
76-
parts of computation occur. We can see that by dropping the `const` we can mutate the
78+
parts of computing occur. We can see that by dropping the `const` we can mutate the
7779
variable freely.
7880

79-
```cpp
81+
```cpp,icon=%cplusplus,fp=main.cxx
8082
#include <iostream>
8183

8284
auto main() -> int {
@@ -90,10 +92,8 @@ auto main() -> int {
9092
}
9193
```
9294

93-
With it compiling to...
94-
95-
```sh
96-
$ cmake -S . -B build --preset=<platform>
95+
```sh,icon=%gnubash,fp=Shell
96+
$ cmake -S . -B build
9797
$ cmake --build build
9898
$ ./build/main
9999
42
@@ -113,7 +113,7 @@ is a `constexpr` and is initialized to some expression; even containing a functi
113113
and another initialized to a simple number but immediately changed to the same expression
114114
value.
115115

116-
```cpp
116+
```cpp,icon=%cplusplus,fp=main.cxx
117117
#include <iostream>
118118

119119
auto constexpr sum(auto const n) {
@@ -139,7 +139,7 @@ auto main() -> int {
139139
140140
This generates the following assembly (at least for GCC-14):
141141
142-
```asm
142+
```haskell,icon=,fp=x86_64
143143
main:
144144
push rbp
145145
mov rbp, rsp
@@ -203,7 +203,7 @@ auto y = 6;
203203
```
204204

205205
`auto` is a keyword that allows the compiler to perform *type deduction*, which means we
206-
tell the compiler to figure out the type of the variable or function return signature
206+
allow the compiler to infer the type of a variable or function return signature
207207
from the context it is given.
208208

209209
## Storage Duration

0 commit comments

Comments
 (0)