1 #include <iostream>
2 #include <cstdlib>
3
4 using namespace std;
5
6 class c
7 {
8 public:
9 c(){ cout << "Default ctor" << endl; }
10 c(const c& o) { cout << "Copy ctor" << endl; }
11 c(c&& o){ cout << "move copy ctor" << endl; }
12 //c(c&& o)=delete;
13 ~c() { cout << "Destructor" << endl; }
14 };
15
16 int main()
17 {
18 c o = c();
19 c p = std::move(c());
20 return 0;
21 }
This piece of code produces the below output.
Default ctor
Default ctor
move copy ctor
Destructor
Destructor
Destructor
However, if I deactivate line#11 and activate line#12 then it gives below compile time error.
prog.cc: In function 'int main()':
prog.cc:18:13: error: use of deleted function 'c::c(c&&)'
18 | c o = c();
| ^
prog.cc:12:9: note: declared here
12 | c(c&& o)=delete;
| ^
which implies that, line#18 invokes the move copy constructor provided by compiler. How to get my user defined move constructor in line#11 invoked without using std::move?
Or std::move is the only way to get user defined move constructors invoked?
Aucun commentaire:
Enregistrer un commentaire