S
SG
Certainly. I thought about rvalue ref qualifiers:
move_iterator<T*> begin() && { return __begin; }
T const* begin() const& { return __begin; }
But it won't work that way because it's an lvalue again when it happens to
sit in the constructor's parameter :/
You can overload the constructor though
template<typename T>
struct container {
container(initializer_list<T> && i) { /* move ... */ }
container(initializer_list<T> const&i) { /* copy ... */ }
};
But it looks a bit weird when seen together with the copy behavior of
initializer_list (no deep copy).
The problem with this approach is that std::initializer_list only
*refers* to an array, it doesn't own it. So, from a conceptual point
of view it acts as a pair of iterators rather than a container.
Whether you get your initializer_object as an lvalue or rvalue is of
no significance because you can simply copy it and the copy refers to
the same array.
container(initializer_list<T> const& i)
{
initializer_list<T> j = i; // non-const copy
}
This reminds me of the futile attempts of learning C++ users to get
away with only one iterator type that is supposed to magically serve
two purposes (const iterator and non-const iterator). The error is to
use one type where two distinct types are needed.
[...]
The draft guarantees that the initializer_list refers to that non-const
array, if "T" is nonconstant (by the text you quoted earlier from 8.5.4/4).
How can it guarantee that it refers to a non-const array when at the
same time it specifically allows a compiler to place the objects into
a ROM?
Cheers!
SG