C++ Library Extensions 2022.12.09
To help learn modern C++ programming
042-cte_rte.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
6
8{
9 private:
10 bool m_valid{true};
11
12 void cleanup()
13 {
14 if(m_valid)
15 {
16 std::cout <<"Object cleaned up" << std::endl;
17 }
18 }
19 void invalidate()
20 {
21 this->m_valid = false;
22 }
23
24 public:
25
27 {
28 std::cout << "Object created" << std::endl;
29 }
30
31 void report()
32 {
33 stream <<"The address is " << this << endl;
34 }
35
37 {
38 std::cout << "Object copy-constructed" << std::endl;
39 }
40
42 {
43 if(this != std::addressof(rhs))
44 {
45 std::cout << "Object copy-assigned" << std::endl;
46 }
47
48 return *this;
49 }
50
52 {
53 std::cout <<"Object moved" << std::endl;
54 rhs.invalidate();
55 }
56
58 {
59 if(this != std::addressof(rhs))
60 {
61 std::cout << "Object move-assigned" << std::endl;
62 }
63
64 return *this;
65 }
66
68 {
69 cleanup();
70 }
71};
72
74{
75 {
76 ClsObject object;
77
78 object.report();
79
80 stream <<"-- Before entering inner block" << endl;
81
82 {
83 ClsObject& lvalue_ref = object;
84
85 lvalue_ref.report();
86
87 ClsObject&& rvalue_ref = std::move(object);
88
89 rvalue_ref.report();
90
91 }
92
93 stream << "-- After leaving inner block" << endl;
94 stream << "IMPORTANT - object is still valid in this scope" << endl;
95 stream << "ClsObject&& rvalue_ref = std::move(object) -- has no effect." << endl;
96 }
97
98 stream <<"\n1. object, lvalue_ref, rvalue_ref are all the same." << endl;
99 stream <<" no copy constructor, no move constructor, no copy assignment, no move assignment" << endL;
100}
101
103{
104 {
105 ClsObject object;
106
107 object.report();
108
109 stream <<"-- Before entering inner block" << endl;
110
111 {
112 ClsObject& lvalue_ref = object;
113
114 lvalue_ref.report();
115
116 ClsObject&& rvalue_ref = std::move(object);
117
118 rvalue_ref.report();
119
120 ClsObject local_object = std::move(lvalue_ref);
121
122 }
123
124 stream <<"-- After leaving inner block" << endl;
125
126 stream << "IMPORTANT: object is now invalid at this point" << endl;
127 stream << "ClsObject local_object = std::move(lvalue_ref) - has serious effect." << endl;
128 }
129
130 stream <<"\n2. object, lvalue_ref, rvalue_ref are all the same." << endl;
131 stream <<" std::move invalidates the original object." << endl;
132}
133
134
135
136int main()
137{
140}
auto endL
Definition: 042-cte_rte.cpp:5
tpf::sstream stream
Definition: 042-cte_rte.cpp:3
void traits_of_reference_two()
auto endl
Definition: 042-cte_rte.cpp:4
int main()
void traits_of_reference_one()
Definition: 042-cte_rte.cpp:73
auto & cout
ClsObject & operator=(const ClsObject &rhs)
Definition: 042-cte_rte.cpp:41
ClsObject(const ClsObject &)
Definition: 042-cte_rte.cpp:36
ClsObject & operator=(ClsObject &&rhs)
Definition: 042-cte_rte.cpp:57
void report()
Definition: 042-cte_rte.cpp:31
ClsObject(ClsObject &&rhs)
Definition: 042-cte_rte.cpp:51
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.