#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "A()" << endl;
}
A(const A& a) {
cout << "A(const A& a)" << endl;
}
A(A&& a) {
cout << "A(A&& a)" << endl;
}
A& operator=(const A& a) {
cout << "operator=(const A& a)" << endl;
return *this;
}
A& operator=(A&& a) {
cout << "operator=(A&& a)" << endl;
return *this;
}
~A() {
cout << "~A()" << endl;
};
};
A foo() {
A a;
return a;
}
int main() {
A a = foo();
}
Compile:Constructing und Destructing von lokalen Variablen und Rück Variable in C++
clang++ test.cpp -o test -std=c++11
Ausgang:
A()
~A()
Warum gibt es nur ein Paar von A() und ~ A() in der Ausgabe?
Warum wird der Move-Konstruktor nicht aufgerufen?
Haben die Compiler eine Code-Optimierung durchgeführt?