Given the following code (http://liveworkspace.org/code/5oact):
class Foo
{
public:
Foo()
{
log(__PRETTY_FUNCTION__);
}
Foo(const Foo& other)
{
log(__PRETTY_FUNCTION__);
}
Foo& operator=(const Foo& other)
{
log(__PRETTY_FUNCTION__);
return *this;
}
Foo(Foo&& other) noexcept
{
log(__PRETTY_FUNCTION__);
}
Foo& operator=(Foo&& other) noexcept
{
log(__PRETTY_FUNCTION__);
return *this;
}
~Foo(){}
};
Using the class like this:
std::vector<Foo> tt;
tt.emplace_back();
tt.emplace_back();
tt.emplace_back();
tt.emplace_back();
I get the following output:
Foo::Foo()
Foo::Foo()
Foo::Foo(const Foo&)
Foo::Foo()
Foo::Foo(const Foo&)
Foo::Foo(const Foo&)
Foo::Foo()
and if I remove the custom destructor I get the following output:
Foo::Foo()
Foo::Foo()
Foo::Foo(Foo&&)
Foo::Foo()
Foo::Foo(Foo&&)
Foo::Foo(Foo&&)
Foo::Foo()
Why the compiler uses the copy constructor instead of the move when I declare a destructor? I understand that the move operation can't throw (and if I remove the noexcept
from the code, the compiler won't use it at all), but what the destructor has to do with that?