I have the following very simple class:
class Foo { public: Foo() {} Foo(const Foo&) = delete; Foo(Foo&&) {} void operator=(const Foo&) = delete; void operator=(Foo&&) {} void dump() const {} };
A class moves constructively and is assigned, but it is not copied constructively and assigned.
I would like to initialize the vector of Foo elements using the vector list of initializers.
std::vector<Foo> vf = { Foo() };
The compiler complains because the code must use the remote copy constructor. Can someone explain why the move construct is not used in this case and why a copy of the object is needed?
The following also requires a copy constructor and does not work:
std::vector<Foo> vf = { std::move(Foo()) };
On the other hand, this works fine (the move constructor is called):
std::vector<Foo> vf; vf.push_back(Foo());
Thanks for the explanation...:)
Update:
The article suggested by this explains my question.
Also, consider the following code (along with class Foo above):
class Bar { public: Bar(std::initializer_list<Foo> _l) { std::cout << "Bar::Bar()" << std::endl; for (auto& e : _l) e.dump(); } }; int main() { Bar b { Foo() }; return 0; }
This creates the following result (compiled with C ++ 11):
Foo::Foo() Bar::Bar() Foo::dump() Foo::~Foo()
You can see that the list of initializers is actually not filled with the "copy of objects" specified between curly braces. This may not be correct from C ++ 14.