Consider the following code:
#include <iostream>
#include <vector>
using namespace std;
class A
{
public:
A(int) { cout << "int" << endl; }
A(A&&) { cout << "move" << endl; }
A(const A&) { cout << "copy" << endl; }
};
int main()
{
vector<A> v
{
A(10), A(20), A(30)
};
_getch();
return 0;
}
The output is:
int
int
int
copy
copy
copy
A(10), A(20), A(30) are temporaries, right?
So why the copy constructor is called? Shouldn't the move constructor be called instead?
Passing move(A(10)), move(A(20)), move(A(30)) instead, the output is:
int
move
int
move
int
move
copy
copy
copy
In this case either copy or move constructor are called. What's happening? Thanks.
Aucun commentaire:
Enregistrer un commentaire