-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRVO.cpp
More file actions
24 lines (20 loc) · 861 Bytes
/
RVO.cpp
File metadata and controls
24 lines (20 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// copy elision(or copy omission): a compiler optimization method, prevents objects from being duplicated/copied
// RVO: return value optimization, a technique that gives the compiler some additional power to terminate the temporary object created
#include <iostream>
using namespace std;
class GfG{
public:
GfG() { cout << "Default constructor called" << endl; }
GfG(const GfG&){
cout << "Copy constructor called" << endl;
}
};
GfG func(){
return GfG(); // RVO: The returned prvalue is constructed directly in the caller’s storage;
// no local temporary is materialized and no copy/move constructor is invoked
}
int main(){
GfG g = func(); // the compiler will decide what to print, could be default constructor once + copy constructor none, once, or twice
getchar();
return 0;
}