mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
rule_of_five
This commit is contained in:
66
README.adoc
66
README.adoc
@@ -2509,6 +2509,9 @@ Note that secondary cores in gem5 are kind of broken however: <<gem5-gdb-step-de
|
|||||||
Bibliography:
|
Bibliography:
|
||||||
|
|
||||||
* https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787
|
* https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787
|
||||||
|
** https://stackoverflow.com/questions/663958/how-to-control-which-core-a-process-runs-on/50210009#50210009
|
||||||
|
** https://stackoverflow.com/questions/280909/cpu-affinity/54478296#54478296
|
||||||
|
** https://unix.stackexchange.com/questions/73/how-can-i-set-the-processor-affinity-of-a-process-on-linux/441098#441098 (summary only)
|
||||||
* https://stackoverflow.com/questions/42800801/how-to-use-gdb-to-debug-qemu-with-smp-symmetric-multiple-processors
|
* https://stackoverflow.com/questions/42800801/how-to-use-gdb-to-debug-qemu-with-smp-symmetric-multiple-processors
|
||||||
|
|
||||||
=== Linux kernel GDB scripts
|
=== Linux kernel GDB scripts
|
||||||
@@ -20225,14 +20228,6 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik
|
|||||||
|
|
||||||
* link:userland/cpp/empty.cpp[]
|
* link:userland/cpp/empty.cpp[]
|
||||||
* link:userland/cpp/hello.cpp[]
|
* link:userland/cpp/hello.cpp[]
|
||||||
* classes
|
|
||||||
** constructor
|
|
||||||
*** link:userland/cpp/initializer_list_constructor.cpp[]: documents stuff like `std::vector<int> v{0, 1};` and `std::initializer_list`
|
|
||||||
*** link:userland/cpp/most_vexing_parse.cpp[]: the most vexing parse is a famous constructor vs function declaration syntax gotcha!
|
|
||||||
**** https://en.wikipedia.org/wiki/Most_vexing_parse
|
|
||||||
**** http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets
|
|
||||||
** `virtual` and polymorphism
|
|
||||||
*** link:userland/cpp/virtual.cpp[]
|
|
||||||
* iostream
|
* iostream
|
||||||
** 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
|
** 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
|
||||||
* fstream
|
* fstream
|
||||||
@@ -20247,6 +20242,61 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik
|
|||||||
*** link:userland/cpp/multimap.cpp[]: `std::multimap`
|
*** link:userland/cpp/multimap.cpp[]: `std::multimap`
|
||||||
** <<algorithms>> contains a benchmark comparison of different c++ containers
|
** <<algorithms>> contains a benchmark comparison of different c++ containers
|
||||||
|
|
||||||
|
[[cpp-classes]]
|
||||||
|
==== C++ classes
|
||||||
|
|
||||||
|
[[cpp-constructor]]
|
||||||
|
===== C++ constructor
|
||||||
|
|
||||||
|
* link:userland/cpp/initializer_list_constructor.cpp[]: documents stuff like `std::vector<int> v{0, 1};` and `std::initializer_list`
|
||||||
|
* link:userland/cpp/most_vexing_parse.cpp[]: the most vexing parse is a famous constructor vs function declaration syntax gotcha!
|
||||||
|
** https://en.wikipedia.org/wiki/Most_vexing_parse
|
||||||
|
** http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets
|
||||||
|
* `virtual` and polymorphism
|
||||||
|
** link:userland/cpp/virtual.cpp[]
|
||||||
|
|
||||||
|
[[cpp-rule-of-five]]
|
||||||
|
====== C++ rule of five
|
||||||
|
|
||||||
|
link:userland/cpp/rule_of_five.cpp[]
|
||||||
|
|
||||||
|
Output Ubuntu 20.04 GCC 9.3:
|
||||||
|
|
||||||
|
....
|
||||||
|
constructor?
|
||||||
|
constructor
|
||||||
|
|
||||||
|
copy?
|
||||||
|
constructor
|
||||||
|
copy
|
||||||
|
|
||||||
|
copy assignment?
|
||||||
|
constructor
|
||||||
|
copy assignment
|
||||||
|
constructor
|
||||||
|
copy
|
||||||
|
move assignment
|
||||||
|
destructor
|
||||||
|
|
||||||
|
move?
|
||||||
|
constructor
|
||||||
|
|
||||||
|
move?
|
||||||
|
constructor
|
||||||
|
constructor
|
||||||
|
move assignment
|
||||||
|
destructor
|
||||||
|
|
||||||
|
a bunch of destructors?
|
||||||
|
destructor
|
||||||
|
destructor
|
||||||
|
destructor
|
||||||
|
destructor
|
||||||
|
destructor
|
||||||
|
....
|
||||||
|
|
||||||
|
https://en.cppreference.com/w/cpp/language/rule_of_three
|
||||||
|
|
||||||
[[cpp-standards]]
|
[[cpp-standards]]
|
||||||
==== C++ standards
|
==== C++ standards
|
||||||
|
|
||||||
|
|||||||
75
userland/cpp/rule_of_five.cpp
Normal file
75
userland/cpp/rule_of_five.cpp
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-rule-of-five
|
||||||
|
// Adapted from https://en.cppreference.com/w/cpp/language/rule_of_five
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
class rule_of_five
|
||||||
|
{
|
||||||
|
char* cstring;
|
||||||
|
public:
|
||||||
|
rule_of_five(const char* s = "")
|
||||||
|
: cstring(nullptr) {
|
||||||
|
std::cout << "constructor" << std::endl;
|
||||||
|
if (s) {
|
||||||
|
std::size_t n = std::strlen(s) + 1;
|
||||||
|
cstring = new char[n];
|
||||||
|
std::memcpy(cstring, s, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~rule_of_five() {
|
||||||
|
std::cout << "destructor" << std::endl;
|
||||||
|
delete[] cstring;
|
||||||
|
}
|
||||||
|
|
||||||
|
rule_of_five(const rule_of_five& other)
|
||||||
|
: rule_of_five(other.cstring) {
|
||||||
|
std::cout << "copy" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
rule_of_five& operator=(const rule_of_five& other) {
|
||||||
|
std::cout << "copy assignment" << std::endl;
|
||||||
|
return *this = rule_of_five(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
rule_of_five(rule_of_five&& other) noexcept
|
||||||
|
: cstring(std::exchange(other.cstring, nullptr)) {
|
||||||
|
std::cout << "move" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
rule_of_five& operator=(rule_of_five&& other) noexcept {
|
||||||
|
std::cout << "move assignment" << std::endl;
|
||||||
|
std::swap(cstring, other.cstring);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << "constructor?" << std::endl;
|
||||||
|
rule_of_five o1{"aaa"};
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "copy?" << std::endl;
|
||||||
|
auto o2{o1};
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "copy assignment?" << std::endl;
|
||||||
|
rule_of_five o3("bbb");
|
||||||
|
o3 = o2;
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "move?" << std::endl;
|
||||||
|
rule_of_five o4(rule_of_five("ccc"));
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "move?" << std::endl;
|
||||||
|
rule_of_five o5("ddd");
|
||||||
|
o5 = rule_of_five("eee");
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "a bunch of destructors?" << std::endl;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user