vendredi 6 mars 2015

Copy Elision Misunderstandment


#include <iostream>

using namespace std;

class A
{
public:

A()
{
cout << "Def Constr" << endl;
}

A(const A&)
{
cout << "Copy Constr" << endl;
}

};

A func1()
{
return A{};
}

void func2(A a)
{
cout << "Hello" << endl;
}


int main()
{
func2(func1());
}


After compiling with



g++ Copy.cpp -std=c++11 -fno-elide-constructors



Output is :



Def Constr


Copy Constr


Copy Constr



And my questions is : Why 2 Copy Constr ? I thought only 1 Copy was needed.


I might have a guess that func1() throws a temp object and this temp object needs to be copied to another memory region and from that region again a copy must be made for the func2() parameter but it's vague for me .


Could you explain it in detail please ?


Aucun commentaire:

Enregistrer un commentaire