lundi 29 juin 2020

Why is copy constructor called rather than move constructor?

I have the following code:

#include <bits/stdc++.h>

using namespace std;

class A {
public:
    A(const A& a) noexcept { cout << "copy constructor" << endl; }
    A& operator=(const A& a) noexcept { cout << "copy assignment operator" << endl; }
    A(A&& a) noexcept { cout << "move constructor" << endl; }
    A& operator=(A&& a) noexcept { cout << "move assignment operator" << endl; }
    A() { cout << "default constructor" << endl; }
};

vector<A> aList;

void AddData(const A&& a)
{
    aList.push_back(std::move(a));
}

int main()
{
    AddData(A());
    return 0;
}

The output is default constructor copy constructor. please tell me is the rvalue reference push_back(T&&)called? And when is copy constructor called?

Aucun commentaire:

Enregistrer un commentaire