-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCopyMoveElision.cpp
More file actions
61 lines (48 loc) · 1.56 KB
/
CopyMoveElision.cpp
File metadata and controls
61 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// =====================================================================================
// CopyMoveElision.cpp // Copy/Move Elision
// =====================================================================================
module modern_cpp:copy_move_elision;
namespace CopyMoveElision {
class Foo
{
private:
int m_value;
public:
Foo() : m_value{} {
std::cout << "c'tor() [" << m_value << "]" << std::endl;
}
Foo(int value) : m_value{ value } {
std::cout << "c'tor (int) [" << m_value << "]" << std::endl;
}
// "Rule-of-Three"
~Foo() {
std::cout << "d'tor [" << m_value << "]" << std::endl;
}
Foo(const Foo& other) {
m_value = other.m_value;
std::cout << "copy-c'tor !!!!!!!!!!! [" << m_value << "]" << std::endl;
}
Foo& operator=(const Foo& other) {
m_value = other.m_value;
std::cout << "operator=" << std::endl;
return *this;
}
};
// test method
static Foo createData() {
Foo data{ 1 };
return data;
// return Foo{ 1 }; // Note: Mandatory copy / move elision
}
static void test_copy_elision() {
Foo data{ createData() };
}
}
void main_copy_move_elision()
{
using namespace CopyMoveElision;
test_copy_elision();
}
// =====================================================================================
// End-of-File
// =====================================================================================