Possible Duplicate:
initializer_list and move semantics
In this code:
#include <vector>
#include <initializer_list>
template<typename T>
class some_custom_container : public std::vector<T>
{
public:
some_custom_container(const std::initializer_list<T>& contents)
{
for (auto& i : contents)
this->emplace_back(std::move(i));
}
};
class test_class
{};
int main()
{
test_class a;
some_custom_container<test_class> i = { a, test_class(), a };
}
If I've understand it, all objects in { a, test_class(), a }
are safe-constructed: the named-objects are copied and the unnamed objects are moved to construct the initializer_list. After, this initializer_list
is passed by reference to the some_custom_container
's constructor.
Then, to avoid useless doble-copies I move all of them to fill the vector.
Is it safe this constructor? I mean, in a strange situation, for example if T is evaluated as a reference & or &&, is the vector always well-filled (contains it safe objects)?
If this is the case, why the initializer_list
constructor implementations of stl containers aren't implemented in this way? As I know, their constructors copy and don't move the contents.