Consider the following code snippet:
#include <iostream>
#include <string>
class A {
public:
A() {
std::cout << "A::A()\n";
}
~A() {
std::cout << "A::~A()\n";
}
A(const A&) = delete;
A(A&&) {
std::cout << "A::A(A&&)\n";
};
};
A f() {
A a;
return a;
}
int main() {
A a = f();
return 0;
}
It compiles fine with g++and clang++, and the output
A::A()
A::~A()
It seems RVO in this case. Note that the move constructor is not called.
However, if you remove this unused move constructor from the above code, and the fragment turns into this:
#include <iostream>
#include <string>
class A {
public:
A() {
std::cout << "A::A()\n";
}
~A() {
std::cout << "A::~A()\n";
}
A(const A&) = delete;
};
A f() {
A a;
return a;
}
int main() {
A a = f();
return 0;
}
Both clang++and g++refuse to compile it because of class copy-constructor A, marked as deleted, so it seems that the RVO is not the case.
How can it remove an unused move constructor?
source
share