std::initializer_list<T>
allocates a temporary array T[]
whose elements are copied from with the list-initializer. It's begin
and end
methods return const T*
. This makes it so that you can't move the elements and you perform yet another copy. However, it's trivial to do
Vector(std::initializer_list<T> IL) :Size{IL.size()}, :Storage{new T[size]} {
T* slot = Storage;
for (auto ele = IL.begin(); ele != IL.end(); ele++, slot++)
*slot = std::move(*const_cast<T*>(ele));
}
I'm sure there's some valid reason why std::initializer_list<T>::begin
returns a const T*
and thus you shouldn't do what's shown above but I don't see it.