Skip to content

Commit fa8c2ee

Browse files
committed
rule_of_five
1 parent 2abb994 commit fa8c2ee

File tree

2 files changed

+133
-8
lines changed

2 files changed

+133
-8
lines changed

README.adoc

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,6 +2509,9 @@ Note that secondary cores in gem5 are kind of broken however: <<gem5-gdb-step-de
25092509
Bibliography:
25102510

25112511
* https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787
2512+
** https://stackoverflow.com/questions/663958/how-to-control-which-core-a-process-runs-on/50210009#50210009
2513+
** https://stackoverflow.com/questions/280909/cpu-affinity/54478296#54478296
2514+
** https://unix.stackexchange.com/questions/73/how-can-i-set-the-processor-affinity-of-a-process-on-linux/441098#441098 (summary only)
25122515
* https://stackoverflow.com/questions/42800801/how-to-use-gdb-to-debug-qemu-with-smp-symmetric-multiple-processors
25132516

25142517
=== Linux kernel GDB scripts
@@ -20225,14 +20228,6 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik
2022520228

2022620229
* link:userland/cpp/empty.cpp[]
2022720230
* link:userland/cpp/hello.cpp[]
20228-
* classes
20229-
** constructor
20230-
*** link:userland/cpp/initializer_list_constructor.cpp[]: documents stuff like `std::vector<int> v{0, 1};` and `std::initializer_list`
20231-
*** link:userland/cpp/most_vexing_parse.cpp[]: the most vexing parse is a famous constructor vs function declaration syntax gotcha!
20232-
**** https://en.wikipedia.org/wiki/Most_vexing_parse
20233-
**** http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets
20234-
** `virtual` and polymorphism
20235-
*** link:userland/cpp/virtual.cpp[]
2023620231
* iostream
2023720232
** link:userland/cpp/copyfmt.cpp[]: `std::copyfmt` restores stream state, see also: https://stackoverflow.com/questions/12560291/set-back-default-floating-point-print-precision-in-c/53673686#53673686
2023820233
* fstream
@@ -20247,6 +20242,61 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik
2024720242
*** link:userland/cpp/multimap.cpp[]: `std::multimap`
2024820243
** <<algorithms>> contains a benchmark comparison of different c++ containers
2024920244

20245+
[[cpp-classes]]
20246+
==== C++ classes
20247+
20248+
[[cpp-constructor]]
20249+
===== C++ constructor
20250+
20251+
* link:userland/cpp/initializer_list_constructor.cpp[]: documents stuff like `std::vector<int> v{0, 1};` and `std::initializer_list`
20252+
* link:userland/cpp/most_vexing_parse.cpp[]: the most vexing parse is a famous constructor vs function declaration syntax gotcha!
20253+
** https://en.wikipedia.org/wiki/Most_vexing_parse
20254+
** http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets
20255+
* `virtual` and polymorphism
20256+
** link:userland/cpp/virtual.cpp[]
20257+
20258+
[[cpp-rule-of-five]]
20259+
====== C++ rule of five
20260+
20261+
link:userland/cpp/rule_of_five.cpp[]
20262+
20263+
Output Ubuntu 20.04 GCC 9.3:
20264+
20265+
....
20266+
constructor?
20267+
constructor
20268+
20269+
copy?
20270+
constructor
20271+
copy
20272+
20273+
copy assignment?
20274+
constructor
20275+
copy assignment
20276+
constructor
20277+
copy
20278+
move assignment
20279+
destructor
20280+
20281+
move?
20282+
constructor
20283+
20284+
move?
20285+
constructor
20286+
constructor
20287+
move assignment
20288+
destructor
20289+
20290+
a bunch of destructors?
20291+
destructor
20292+
destructor
20293+
destructor
20294+
destructor
20295+
destructor
20296+
....
20297+
20298+
https://en.cppreference.com/w/cpp/language/rule_of_three
20299+
2025020300
[[cpp-standards]]
2025120301
==== C++ standards
2025220302

userland/cpp/rule_of_five.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-rule-of-five
2+
// Adapted from https://en.cppreference.com/w/cpp/language/rule_of_five
3+
4+
#include <cstdlib>
5+
#include <cstring>
6+
#include <iostream>
7+
#include <utility>
8+
9+
class rule_of_five
10+
{
11+
char* cstring;
12+
public:
13+
rule_of_five(const char* s = "")
14+
: cstring(nullptr) {
15+
std::cout << "constructor" << std::endl;
16+
if (s) {
17+
std::size_t n = std::strlen(s) + 1;
18+
cstring = new char[n];
19+
std::memcpy(cstring, s, n);
20+
}
21+
}
22+
23+
~rule_of_five() {
24+
std::cout << "destructor" << std::endl;
25+
delete[] cstring;
26+
}
27+
28+
rule_of_five(const rule_of_five& other)
29+
: rule_of_five(other.cstring) {
30+
std::cout << "copy" << std::endl;
31+
}
32+
33+
rule_of_five& operator=(const rule_of_five& other) {
34+
std::cout << "copy assignment" << std::endl;
35+
return *this = rule_of_five(other);
36+
}
37+
38+
rule_of_five(rule_of_five&& other) noexcept
39+
: cstring(std::exchange(other.cstring, nullptr)) {
40+
std::cout << "move" << std::endl;
41+
}
42+
43+
rule_of_five& operator=(rule_of_five&& other) noexcept {
44+
std::cout << "move assignment" << std::endl;
45+
std::swap(cstring, other.cstring);
46+
return *this;
47+
}
48+
};
49+
50+
int main()
51+
{
52+
std::cout << "constructor?" << std::endl;
53+
rule_of_five o1{"aaa"};
54+
std::cout << std::endl;
55+
56+
std::cout << "copy?" << std::endl;
57+
auto o2{o1};
58+
std::cout << std::endl;
59+
60+
std::cout << "copy assignment?" << std::endl;
61+
rule_of_five o3("bbb");
62+
o3 = o2;
63+
std::cout << std::endl;
64+
65+
std::cout << "move?" << std::endl;
66+
rule_of_five o4(rule_of_five("ccc"));
67+
std::cout << std::endl;
68+
69+
std::cout << "move?" << std::endl;
70+
rule_of_five o5("ddd");
71+
o5 = rule_of_five("eee");
72+
std::cout << std::endl;
73+
74+
std::cout << "a bunch of destructors?" << std::endl;
75+
}

0 commit comments

Comments
 (0)