I have this piece of code:
#include <iostream>
#include <vector>
using namespace std;
class Foo{
public:
Foo() noexcept {cout << "ctor" << endl;}
Foo(const Foo&) noexcept {cout << "copy ctor" << endl;}
Foo(Foo&&) noexcept {cout << "move ctor" << endl;}
Foo& operator=(Foo&&) noexcept {cout << "move assn" << endl; return *this;}
Foo& operator=(const Foo&) noexcept {cout << "copy assn" << endl; return *this;}
~Foo() noexcept {cout << "dtor" << endl;}
};
int main()
{
Foo foo;
vector<Foo> v;
v.push_back(std::move(foo));
// comment the above 2 lines and replace by
// vector<Foo> v{std::move(foo)};
}
The output is what I expect (compiled with g++ -std=c++11 --no-elide-constructors
, same output without the flag)
ctor
move ctor
dtor
dtor
Now instead of using push_back
initialize directly the vector v
as
vector<Foo> v{std::move(foo)};
I do not understand why I get the outputs:
1) (without --no-elide-constructors
)
ctor
move ctor
copy ctor
dtor
dtor
dtor
2) (with --no-elide-constructors
)
ctor
move ctor
move ctor
copy ctor
dtor
dtor
dtor
dtor
In the first case, why is the copy ctor invoked? And in the second case, when the compiler does not perform elision, I have absolutely no idea why the move ctor is invoked twice. Any ideas?
{...}
for uniform initialization is quite tricky, and I now realized thatvector
is not an aggregate. And yes, same on my clang, no extra move. Great answer, thanks! – Edgebone