diff options
Diffstat (limited to '')
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | README.md | 7 | ||||
| -rw-r--r-- | raii/all-constructors.cpp | 94 | ||||
| -rw-r--r-- | raii/allocate.cpp | 8 | ||||
| -rw-r--r-- | raii/constructor-hello-world.cpp | 17 | ||||
| -rw-r--r-- | raii/surprising-error.cpp | 11 |
6 files changed, 138 insertions, 0 deletions
| @@ -1 +1,2 @@ | |||
| 1 | sort-benchmark/*/sort | 1 | sort-benchmark/*/sort |
| 2 | */a.out | ||
| @@ -4,6 +4,9 @@ Experiments with C++ and comparisons with C. | |||
| 4 | 4 | ||
| 5 | ## sort-benchmark | 5 | ## sort-benchmark |
| 6 | 6 | ||
| 7 | For the | ||
| 8 | [first post in the series](https://sebastiano.tronto.net/blog/2024-04-30-taming-cpp-motivation/). | ||
| 9 | |||
| 7 | Inspired by [Bert's post](https://berthub.eu/articles/posts/c++-1). | 10 | Inspired by [Bert's post](https://berthub.eu/articles/posts/c++-1). |
| 8 | 11 | ||
| 9 | Sorting an array of integers is a simple task, so C and C++ should perform | 12 | Sorting an array of integers is a simple task, so C and C++ should perform |
| @@ -17,3 +20,7 @@ C++ time for 100000000 numbers (parallel): 0.495435s | |||
| 17 | 20 | ||
| 18 | A similar benchmark is provided for sorting an arrays of pairs in a | 21 | A similar benchmark is provided for sorting an arrays of pairs in a |
| 19 | non-standard way. | 22 | non-standard way. |
| 23 | |||
| 24 | ## raii | ||
| 25 | |||
| 26 | Various small examples on constructors, destructors and related things. | ||
diff --git a/raii/all-constructors.cpp b/raii/all-constructors.cpp new file mode 100644 index 0000000..27add07 --- /dev/null +++ b/raii/all-constructors.cpp | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | #include <iostream> | ||
| 2 | |||
| 3 | class T { | ||
| 4 | public: | ||
| 5 | T(const std::string& a) : name{a} { | ||
| 6 | std::cout << "Called constructor for " << name | ||
| 7 | << " (" << this << ")" << std::endl; | ||
| 8 | } | ||
| 9 | |||
| 10 | ~T() { | ||
| 11 | std::cout << "Called destructor for " << name | ||
| 12 | << " (" << this << ")" << std::endl; | ||
| 13 | } | ||
| 14 | |||
| 15 | T(T& other) : name{other.name} { | ||
| 16 | std::cout << "Copy constructor called for " << name | ||
| 17 | << " (" << this << " copied from " | ||
| 18 | << &other << ")" << std::endl; | ||
| 19 | } | ||
| 20 | |||
| 21 | T& operator=(T& other) { | ||
| 22 | name = other.name; | ||
| 23 | std::cout << "Copy assignment called for " << name | ||
| 24 | << " (" << this << " copied from " | ||
| 25 | << &other << ")" << std::endl; | ||
| 26 | return *this; | ||
| 27 | } | ||
| 28 | |||
| 29 | T(T&& other) : name{other.name} { | ||
| 30 | std::cout << "Move constructor called for " << name | ||
| 31 | << " (" << this << " moved from " | ||
| 32 | << &other << ")" << std::endl; | ||
| 33 | } | ||
| 34 | |||
| 35 | T& operator=(T&& other) { | ||
| 36 | name = other.name; | ||
| 37 | std::cout << "Move assignment called for " << name | ||
| 38 | << " (" << this << " moved from " | ||
| 39 | << &other << ")" << std::endl; | ||
| 40 | return *this; | ||
| 41 | } | ||
| 42 | private: | ||
| 43 | std::string name; | ||
| 44 | }; | ||
| 45 | |||
| 46 | T do_nothing_and_return(T x) { | ||
| 47 | return x; | ||
| 48 | } | ||
| 49 | |||
| 50 | T create(const std::string& str) { | ||
| 51 | return T(str); | ||
| 52 | } | ||
| 53 | |||
| 54 | int main() { | ||
| 55 | //T bad; // Compile-time error: no default constructor. | ||
| 56 | |||
| 57 | std::cout << "---------- Declaring a simple variable ----------" << std::endl; | ||
| 58 | T a("A"); | ||
| 59 | std::cout << std::endl; | ||
| 60 | |||
| 61 | // Now we can see that objects are indeed scope-bound: they | ||
| 62 | // are destroyed when they go out of scope. | ||
| 63 | std::cout << "--- Declaring a variable in a temporary scope ---" << std::endl; | ||
| 64 | { | ||
| 65 | std::cout << "Entering temporary scope" << std::endl; | ||
| 66 | T b("short-lived"); | ||
| 67 | std::cout << "Exiting temporary scope" << std::endl; | ||
| 68 | } | ||
| 69 | std::cout << "Out of temporary scope" << std::endl; | ||
| 70 | std::cout << std::endl; | ||
| 71 | |||
| 72 | std::cout << "------------ Constructing by copying ------------" << std::endl; | ||
| 73 | T b("B"); | ||
| 74 | T c(b); | ||
| 75 | T d = b; // This is also a copy constructor, not a copy assignment | ||
| 76 | std::cout << std::endl; | ||
| 77 | |||
| 78 | // Copy assignment operators differ from copy constructors in that they | ||
| 79 | // should also clean up the resources for the copied-to object. | ||
| 80 | std::cout << "---------------- Copy assignment ----------------" << std::endl; | ||
| 81 | c = b; | ||
| 82 | std::cout << std::endl; | ||
| 83 | |||
| 84 | std::cout << "------------ Constructing by moving -------------" << std::endl; | ||
| 85 | T e(do_nothing_and_return(b)); | ||
| 86 | std::cout << std::endl; | ||
| 87 | |||
| 88 | std::cout << "---------------- Move assignment ----------------" << std::endl; | ||
| 89 | e = create("E"); | ||
| 90 | std::cout << std::endl; | ||
| 91 | |||
| 92 | std::cout << "------------- Destroying everything -------------" << std::endl; | ||
| 93 | return 0; | ||
| 94 | } | ||
diff --git a/raii/allocate.cpp b/raii/allocate.cpp new file mode 100644 index 0000000..519f09f --- /dev/null +++ b/raii/allocate.cpp | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | bool f(int big_number) { | ||
| 2 | int *a = new int[big_number]; | ||
| 3 | return true; | ||
| 4 | } | ||
| 5 | |||
| 6 | int main() { | ||
| 7 | return 0; | ||
| 8 | } | ||
diff --git a/raii/constructor-hello-world.cpp b/raii/constructor-hello-world.cpp new file mode 100644 index 0000000..c86e1ca --- /dev/null +++ b/raii/constructor-hello-world.cpp | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #include <iostream> | ||
| 2 | |||
| 3 | class BadType { | ||
| 4 | public: | ||
| 5 | BadType() { | ||
| 6 | std::cout << "Variable created!" << std::endl; | ||
| 7 | } | ||
| 8 | |||
| 9 | ~BadType() { | ||
| 10 | std::cout << "Variable destroyed, bye bye" << std::endl; | ||
| 11 | } | ||
| 12 | }; | ||
| 13 | |||
| 14 | int main() { | ||
| 15 | BadType x; | ||
| 16 | return 0; | ||
| 17 | } | ||
diff --git a/raii/surprising-error.cpp b/raii/surprising-error.cpp new file mode 100644 index 0000000..b2e7831 --- /dev/null +++ b/raii/surprising-error.cpp | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | class BadType { | ||
| 2 | public: | ||
| 3 | BadType() { | ||
| 4 | BadType recursion_hell; | ||
| 5 | } | ||
| 6 | }; | ||
| 7 | |||
| 8 | int main() { | ||
| 9 | BadType x; | ||
| 10 | return 0; | ||
| 11 | } | ||
