Skip to content

Commit f8a0aaf

Browse files
committed
revamp for cr session2
1 parent fd01b24 commit f8a0aaf

20 files changed

+278
-221
lines changed

img/lukin.jpg

116 KB
Loading

module3/00-intro.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Something about you
2+
3+
* What you don't like in C++?
4+
* What other programming languages do you know?
5+
6+
___
7+
<!-- .slide: data-background="../img/lukin.jpg" -->
8+
9+
<h2 style="text-shadow: 2px 2px black;">Łukasz Ziobroń</h2>
10+
<div class="box fragment" style="width: 45%; left: 0; top: 100px;">
11+
12+
### Not only a programming XP
13+
14+
* Front-end dev, DevOps & Owner @ Coders School
15+
* C++ and Python developer @ Nokia & Credit Suisse
16+
* Team leader & Trainer @ Nokia
17+
* Scrum Master @ Nokia & Credit Suisse
18+
* Code Reviewer @ Nokia
19+
* Web developer (HTML, PHP, CSS) @ StarCraft Area
20+
21+
</div>
22+
23+
<div class="box fragment" style="width: 45%; right: 0; top: 100px;">
24+
25+
### Experience as a trainer
26+
27+
* <a href="https://coders.school/kurs-online">C++ online course</a> @ Coders School
28+
* Company trainings @ Coders School
29+
* Practical Aspects Of Software Engineering @ PWr & UWr
30+
* Nokia Academy @ Nokia
31+
32+
</div>
33+
34+
<div class="box fragment" style="width: 45%; left: 0; top: 400px;">
35+
36+
### Public speaking experience
37+
38+
* code::dive conference
39+
* code::dive community
40+
* Academic Championships in Team Programming
41+
* <a href="http://youtube.com/c/CodersSchool">Coders School YouTube channel</a>
42+
43+
</div>
44+
45+
<div class="box fragment" style="width: 45%; right: 0; top: 400px;">
46+
47+
### Hobbies
48+
49+
* StarCraft Brood War & StarCraft II
50+
* Motorcycles
51+
* Photography
52+
* Archery
53+
* Andragogy
54+
55+
</div>
56+
57+
___
58+
59+
## Contract
60+
61+
* <!-- .element: class="fragment fade-in" --> 🎰 Vegas rule
62+
* <!-- .element: class="fragment fade-in" --> 🗣 Discussion, not a lecture
63+
* <!-- .element: class="fragment fade-in" --> ☕️ Additional breaks on demand
64+
* <!-- .element: class="fragment fade-in" --> ⌚️ Be on time after breaks

module3/01-pretest.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Pre-test 📝
2+
3+
___
4+
5+
### Question 1/2
6+
7+
We have only the below template function defined.
8+
What will happen in each case? Which example will compile and display "OK"?
9+
10+
```cpp
11+
template <typename T>
12+
void foo(T && a) {std::cout << "OK\n"; }
13+
14+
int a = 5;
15+
```
16+
17+
1. <code>foo(4);</code>
18+
2. <code>foo(a);</code>
19+
3. <code>foo(std::move(a));</code>
20+
21+
___
22+
23+
### Question 2/2
24+
25+
What will be printed on the screen?
26+
27+
```cpp
28+
class Gadget {};
29+
void f(const Gadget&) { std::cout << "const Gadget&\n"; }
30+
void f(Gadget&) { std::cout << "Gadget&\n"; }
31+
void f(Gadget&&) { std::cout << "Gadget&&\n"; }
32+
33+
template <typename Gadget>
34+
void use(Gadget&& g) { f(g); }
35+
36+
int main() {
37+
const Gadget cg;
38+
Gadget g;
39+
use(cg);
40+
use(g);
41+
use(Gadget());
42+
}
43+
```

module3/move_semantics_rvalues_lvalues.md renamed to module3/02-rvalues-lvalues.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ str1 += str2 <span class="fragment">// l-value</span>
7070
str1 + str2 <span class="fragment">// r-value</span>
7171
[](int x){ return x * x; }; <span class="fragment">// r-value</span>
7272
std::move(a); <span class="fragment">// r-value</span>
73-
int && a = 4; <span class="fragment">// 4 is r-value</span>
73+
int && a = 4; <span class="fragment">// 4 is an r-value</span>
7474
</code></pre>
7575

7676
___
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
<!-- .slide: style="font-size: 0.9em" -->
2+
13
## Usage of move semantics
24

35
```cpp
46
template <typename T>
57
class Container {
68
public:
7-
void insert(const T& item); // inserts a copy of item
8-
void insert(T&& item); // moves item into container
9+
void insert(const T& item); // inserts a copy of an item
10+
void insert(T&& item); // moves item into the container
911
};
1012

1113
Container<std::string> c;
@@ -14,11 +16,11 @@ std::string str = "text";
1416
c.insert(str); // lvalue -> insert(const std::string&)
1517
// inserts a copy of str, str is used later
1618
c.insert(str + str); // rvalue -> insert(string&&)
17-
// moves temporary into container
19+
// moves temporary into the container
1820
c.insert("text"); // rvalue -> insert(string&&)
19-
// moves temporary into container
21+
// moves temporary into the container
2022
c.insert(std::move(str)); // rvalue -> insert(string&&)
21-
// moves str into container, str is no longer used
23+
// moves str into the container, str is no longer used
2224
```
2325
2426
___
@@ -28,12 +30,12 @@ ___
2830
* <!-- .element: class="fragment fade-in" --> Transfer all data from the source to the target
2931
* <!-- .element: class="fragment fade-in" --> Leave the source object in an unknown, but safe to delete state
3032
* <!-- .element: class="fragment fade-in" --> The source object should never be used
31-
* <!-- .element: class="fragment fade-in" --> The source object can only be safely destroyed or, if possible, new resource can be assigned to it (eg. <code>reset()</code>)
33+
* <!-- .element: class="fragment fade-in" --> The source object can only be safely destroyed or, if possible, a new resource can be assigned to it (eg. <code>reset()</code>)
3234
3335
```cpp
3436
std::unique_ptr<int> pointer1{new int{5}};
3537
std::unique_ptr<int> pointer2 = std::move(pointer1);
36-
*pointer1 = 4; // Undefined behavior, pointer1 is in moved-from state
38+
*pointer1 = 4; // Undefined behaviour, pointer1 is in the moved-from state
3739
pointer1.reset(new int{20}); // OK
3840
```
3941
<!-- .element: class="fragment fade-in" -->

module3/move_semantics_implementation.md renamed to module3/04-implementation.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- .slide: style="font-size: 0.9em" -->
2+
13
## Implementation of move semantic
24

35
```cpp
@@ -38,14 +40,11 @@ ___
3840
3941
## Task
4042
41-
Aim: learn how to implement move semantics with manual resource management
42-
43-
Write your own implementation of `unique_ptr`
43+
Write your implementation of `unique_ptr`
4444
45-
Let's try online Coding Dojo :)
46-
<!-- .element: class="fragment highlight-green" -->
45+
Aim: learn how to implement move semantics with manual resource management
4746
48-
#### Hints
47+
### Hints
4948
5049
<!-- .element: class="fragment fade-in" -->
5150
@@ -54,7 +53,7 @@ Let's try online Coding Dojo :)
5453
* <!-- .element: class="fragment fade-in" --> Copy operations not allowed
5554
* <!-- .element: class="fragment fade-in" --> Move operations allowed
5655
* <!-- .element: class="fragment fade-in" --> <a href="https://en.cppreference.com/w/cpp/memory/unique_ptr">Interface functions</a> - at least:
57-
* <code>T* get() const noexcept</code>
58-
* <code>T& operator*() const</code>
59-
* <code>T* operator->() const noexcept</code>
60-
* <code>void reset(T* = nullptr) noexcept</code>
56+
* <code>T* get() const noexcept</code>
57+
* <code>T& operator*() const</code>
58+
* <code>T* operator->() const noexcept</code>
59+
* <code>void reset(T* = nullptr) noexcept</code>

module3/move_semantics_rules.md renamed to module3/05-rules.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ If you define at least one of:
44
<!-- .element: class="fragment fade-in" -->
55

66
* <!-- .element: class="fragment fade-in" --> destructor
7-
* <!-- .element: class="fragment fade-in" --> copy constructor
7+
* <!-- .element: class="fragment fade-in" --> copy constructor
88
* <!-- .element: class="fragment fade-in" --> copy assignment operator
99

1010
it means that you are manually managing resources and <span class="fragment highlight-red">you should implement them all</span>.
@@ -20,7 +20,7 @@ ___
2020
Rule of 5 = Rule of 3 + optimizations
2121

2222
* destructor
23-
* copy constructor
23+
* copy constructor
2424
* copy assignment operator
2525
* <!-- .element: class="fragment highlight-green" --> move constructor
2626
* <!-- .element: class="fragment highlight-green" --> move assignment operator
@@ -38,7 +38,7 @@ Do not implement any of Rule of 5 functions 😎
3838
If you use RAII handlers (like smart pointers), all the copy and move operations will be generated (or deleted) implicitly.
3939
<!-- .element: class="fragment fade-in" -->
4040

41-
Eg. when you have unique_ptr as your class member, copy operations of your class will be automatically blocked, but move operations will be supported.
41+
For example, when you have a `unique_ptr` as your class member, copy operations of your class will be automatically blocked, but move operations will be supported.
4242
<!-- .element: class="fragment fade-in" -->
4343

4444
___
@@ -47,12 +47,12 @@ ___
4747

4848
Aim: learn how to refactor code to use RAII and Rule of 0
4949

50-
Write a template class which holds a pointer
50+
Write a template class that holds a pointer
5151

52-
* <!-- .element: class="fragment fade-in" --> use raw pointer to manage resource of a template type
52+
* <!-- .element: class="fragment fade-in" --> use a raw pointer to manage the resource of a template type
5353
* <!-- .element: class="fragment fade-in" --> implement constructor to acquire a resource
54-
* <!-- .element: class="fragment fade-in" --> implement Rule of 3
55-
* <!-- .element: class="fragment fade-in" --> implement Rule of 5
56-
* <!-- .element: class="fragment fade-in" --> implement Rule of 0
57-
* use proper smart pointer instead of raw pointer
54+
* <!-- .element: class="fragment fade-in" --> implement the Rule of 3
55+
* <!-- .element: class="fragment fade-in" --> implement the Rule of 5
56+
* <!-- .element: class="fragment fade-in" --> implement the Rule of 0
57+
* use a roper smart pointer instead of the raw pointer
5858
<!-- .element: class="fragment fade-in" -->

module3/move_semantics_std_move.md renamed to module3/06-std-move.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
## Implementation of `std::move()` and "universal reference"
1+
## Implementation of `std::move()`
2+
3+
### "universal reference"
24

35
```cpp
46
template <typename T>

module3/07-reference-collapsing.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Reference collapsing rules
2+
3+
* <!-- .element: class="fragment fade-in" --> <code>T& &</code> -> <code>T&</code>
4+
* <!-- .element: class="fragment fade-in" --> <code>T& &&</code> -> <code>T&</code>
5+
* <!-- .element: class="fragment fade-in" --> <code>T&& &</code> -> <code>T&</code>
6+
* <!-- .element: class="fragment fade-in" --> <code>T&& &&</code> -> <code>T&&</code>
7+
8+
___
9+
10+
## Reference collapsing
11+
12+
When a template is being instantiated reference collapsing may occur
13+
14+
```cpp
15+
template <typename T>
16+
void f(T & item) {} // takes item always as an l-value reference
17+
18+
void f(int& & item); // passing int& as a param, like f(a) -> f(int&)
19+
void f(int&& & item); // passing int&& as a param, like f(5) -> f(int&)
20+
```
21+
<!-- .element: class="fragment fade-in" -->
22+
23+
```cpp
24+
template <typename T>
25+
void g(T && item) {} // takes item as a forwarding reference
26+
27+
void g(int& && item); // passing int& as a param, like g(a) -> f(int&)
28+
void g(int&& && item); // passing int&& as a param, like g(5) -> f(int&&)
29+
```
30+
<!-- .element: class="fragment fade-in" -->

module3/move_semantics_std_forward.md renamed to module3/08-std-forward.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
<!-- .slide: style="font-size: 0.9em" -->
2+
13
## Interface bloat
24

3-
Trying to optimize for every possible use case may lead to an interface bloat
5+
Trying to optimize for every possible use case may lead to an interface bloat.
46

57
```cpp
68
class Gadget;
@@ -20,14 +22,17 @@ int main() {
2022
}
2123
```
2224
23-
Task: Try to improve the `use()` function to catch more types of reference to have less overloads.
25+
### Task
26+
27+
Improve the `use()` function to catch more types of references to have fewer overloads.
2428
<!-- .element: class="fragment fade-in" -->
2529
2630
___
2731
28-
## Perfect Forwarding
32+
## Solution: Perfect Forwarding
2933
3034
Forwarding reference `T&&` + `std::forward()` is a solution to interface bloat.
35+
<!-- .element: class="fragment fade-in" -->
3136
3237
```cpp
3338
class Gadget;
@@ -49,36 +54,37 @@ int main() {
4954
use(Gadget()); // calls use(Gadget&&) then calls f(Gadget&&)
5055
}
5156
```
57+
<!-- .element: class="fragment fade-in" -->
5258

5359
___
5460

5561
## `std::forward`
5662

57-
Forwarding reference (even bind to r-value) is treated as l-value inside template function
63+
Forwarding reference (even bind to r-value) is treated as l-value inside a template function.
5864

5965
```cpp
6066
template <typename T>
6167
void use(T&& t) {
62-
f(t); // t is treated as l-value unconditionally
68+
f(t); // t treated as l-value unconditionally
6369
}
6470
```
6571
<!-- .element: class="fragment fade-in" -->
6672
6773
```cpp
6874
template <typename T>
6975
void use(T&& t) {
70-
f(std::move(t)); // t is treated as r-value unconditionally
76+
f(std::move(t)); // t treated as r-value unconditionally
7177
}
7278
```
7379
<!-- .element: class="fragment fade-in" -->
7480

7581
```cpp
7682
template <typename T>
77-
void use(T&& t) { // pass t as r-value if r-value was passed,
78-
f(std::forward(t)); // pass as l-value otherwise
83+
void use(T&& t) { // forwards t as r-value if r-value was passed,
84+
f(std::forward(t)); // forwards as l-value otherwise
7985
}
8086
```
8187
<!-- .element: class="fragment fade-in" -->
8288
83-
In other words: `std::forward()` restores original reference type.
89+
In other words, `std::forward()` restores the original reference type.
8490
<!-- .element: class="fragment fade-in" -->

module3/move_semantics_copy_elision.md renamed to module3/09-copy-elision.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ T x = T(T(f())); // only one call to default c-tor of T, to initialize x
1616
```
1717
<!-- .element: class="fragment fade-in" -->
1818

19-
* <!-- .element: class="fragment fade-in" --> in return statement, when the object is temporary (RVO - Return Value Optimisation)
19+
* <!-- .element: class="fragment fade-in" --> in the <code>return</code> statement, when the object is temporary (RVO - Return Value Optimisation)
2020
* <!-- .element: class="fragment fade-in" --> in the initialization, when the initializer is of the same class and is temporary
2121

2222
Do not try to "optimize" code by writing `return std::move(sth);`. It may prevent optimizations.

0 commit comments

Comments
 (0)