Skip to content

Commit a36ccd1

Browse files
authored
Copy semantics (#55)
Integrated first version of Copy Semantics
1 parent 64d0b7d commit a36ccd1

7 files changed

+167
-0
lines changed

object-model/constant-objects.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C++ object model: constant objects
2+
3+
This topic is currently under construction and will soon be filled with information :)

object-model/copy-semantics.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# C++ object model: copy semantics
2+
_Skeleton instructions are typeset in italic text._
3+
4+
## Overview
5+
6+
_Provides a short natural language abstract of the module’s contents._
7+
_Specifies the different levels of teaching._
8+
9+
<table>
10+
<thead>
11+
<th>Level</th>
12+
<th>Objectives</th>
13+
</thead>
14+
<tr>
15+
<td>Foundational</td>
16+
<td>understanding how and when are copies made</td>
17+
</tr>
18+
<tr>
19+
<td>Main</td>
20+
<td>implementing user-defined copy operations</td>
21+
</tr>
22+
<tr>
23+
<td>Advanced</td>
24+
<td>special cases: copy elision</td>
25+
</tr>
26+
</table>
27+
28+
## Motivation
29+
30+
_Why is this important?_
31+
_Why do we want to learn/teach this topic?_
32+
33+
Copy semantics allows the user to define how objects of a class get replicated and interact on a value level.
34+
35+
## Topic introduction
36+
37+
_Very brief introduction to the topic._
38+
39+
Explains when and how objects are copied.
40+
41+
## Foundational: How and when are copies made
42+
43+
### Background/Required Knowledge
44+
45+
A student is able to:
46+
* explain what a C++ type is? [[C++ object model: types]][1]
47+
* explain what an object is? [[C++ object model: objects]][2], [[C++ object model: constant objects]][3]
48+
* define and understand class invariants?
49+
50+
It helps when a student is able to:
51+
* use move semantics [[C++ object model: move semantics]][4]
52+
* explain special member functions [[C++ object model: special member functions]][5]
53+
54+
### Student outcomes
55+
56+
_A list of things "a student should be able to" after the curriculum._
57+
_The next word should be an action word and testable in an exam._
58+
_Max 5 items._
59+
60+
A student should be able to:
61+
62+
1. explain what copy semantics accomplish
63+
* establishing "equivalent" object state in another object
64+
2. explain difference between copying a reference and copying a value\*
65+
3. explain where copies are made
66+
67+
\* In other languages these differences are sometimes referred to as shallow and deep copy.
68+
69+
### Caveats
70+
71+
_This section mentions subtle points to understand, like anything resulting in
72+
implementation-defined, unspecified, or undefined behavior._
73+
74+
* Compiler-provided copy operations may result in ownership problems (e.g., `char*`). These ownership problems can generally be solved by using types whose copy operations have the appropriate semantics, e.g., `std::string` instead of `char*` to hold string values.
75+
76+
### Points to cover
77+
78+
_This section lists important details for each point._
79+
80+
* Principle of copying
81+
* Copying of types, which follow the rule of zero
82+
* Copying of types, with user defined copy operations
83+
* Copying an object does not change the original
84+
* Practical applications
85+
* `std::unique_ptr` (has no copy)
86+
* Strings (copies the value)
87+
88+
89+
## Main: Implementing user-defined copy operations
90+
91+
### Background/Required Knowledge
92+
93+
A student is able to:
94+
* identify special member functions [[C++ object model: special member functions]][5]
95+
96+
It helps when a student is able to:
97+
* use move semantics [[C++ object model: move semantics]][4]
98+
* explain the rule of zero [[C++ object model: rule-of-zero]][7]
99+
* explain the rule of five [[C++ object model: rule-of-five]][6]
100+
101+
### Student outcomes
102+
103+
_A list of things "a student should be able to" after the curriculum._
104+
_The next word should be an action word and testable in an exam._
105+
_Max 5 items._
106+
107+
A student should be able to:
108+
* explain when they have to implement the copy operations for their own type
109+
* Copy constructor
110+
* Copy assignment operator
111+
* implement copy operations for their own types
112+
* _Optional_: explain when copying with basic and strong exception guarantees is useful
113+
114+
### Caveats
115+
116+
_This section mentions subtle points to understand, like anything resulting in
117+
implementation-defined, unspecified, or undefined behavior._
118+
119+
* Intricacies when implementing copy operations:
120+
* Examples of how _not_ to write copy operations (e.g., C++03 `std::auto_ptr`)
121+
122+
### Points to cover
123+
124+
_This section lists important details for each point._
125+
126+
* Copy constructors and copy assignment operators
127+
* How compiler generates default copy operations
128+
* =default, =delete (No copy)
129+
* How-to write your own copy operations
130+
* Rule-of-five
131+
* Copy assignment operators can be ref-qualified to avoid assigning into temporary objects.
132+
133+
## Advanced
134+
135+
_These are not expected to be covered but provide guidance where one can
136+
continue to investigate this topic in more depth._
137+
138+
When can copies be elided and when does the standard guarantee copy elision.
139+
References:
140+
* [Abseil tip of the Week #166](https://abseil.io/tips/166)
141+
* [cppreference - Copy elision](https://en.cppreference.com/w/cpp/language/copy_elision)
142+
143+
[1]: ../object-model/types.md
144+
[2]: ../object-model/objects.md
145+
[3]: ../object-model/constant-objects.md
146+
[4]: ../object-model/move-semantics.md
147+
[5]: ../object-model/special-member-functions.md
148+
[6]: ../object-model/rule-of-five.md
149+
[7]: ../object-model/rule-of-zero.md

object-model/move-semantics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C++ object model: move semantics
2+
3+
This topic is currently under construction and will soon be filled with information :)

object-model/objects.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C++ object model: objects
2+
3+
This topic is currently under construction and will soon be filled with information :)

object-model/rule-of-five.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C++ object model: rule of five
2+
3+
This topic is currently under construction and will soon be filled with information :)

object-model/rule-of-zero.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C++ object model: rule of zero
2+
3+
This topic is currently under construction and will soon be filled with information :)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# C++ object model: special member functions
2+
3+
This topic is currently under construction and will soon be filled with information :)

0 commit comments

Comments
 (0)