-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyElision.cpp
More file actions
40 lines (34 loc) · 1.13 KB
/
CopyElision.cpp
File metadata and controls
40 lines (34 loc) · 1.13 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
#include <iostream>
using namespace std;
class B {
public:
B(const char* str = "\0"){
cout << "Parameterized constructor called" << endl;
}
B(const B& b){
cout << "Copy construcotr called" << endl;
}
};
int main(){
/*
According to theory, when the object "ob" is being constructed,
one argument constructor is used to convert "copy me" to a temporary object &
that temporary object is copied to the object "ob". So the statement
B ob = "copy me"; // Also RVO form to represent
should be broken down by the compiler as:
B ob = B("copy me"); // Also NRVO form to represent
However, most C++ compilers avoid such overheads of creating a temporary object & then copying it.
The modern compilers break down the statement
B ob = "copy me"; //copy initialization
as
B ob("copy me"); //direct initialization
and thus eliding call to copy constructor.
*/
B ob = "copy me";
return 0;
}
/*
If we compile with:
g++ CopyElision.cpp -fno-elide-constructors
the compiler doesn't elide the call to copy construct(disable the copy elision)
*/