STL containers have two overloads for operator[]
, one const
and one non-const
and two corresponding typedef.
template<class T>
struct vector{
T* data_;
using value_type = T;
using reference = T&;
using const_reference = T const&;
reference operator[](int idx){return *(data_ + idx);}
const_reference operator[](int idx) const{return *(data_ + idx);}
};
In C++11, wouldn't for consistency make sense to have a r-value reference return as well?
template<class T>
struct vector{
T* data_;
using value_type = T;
using reference = T&;
using const_reference = T const&;
using move_reference = T&&;
reference operator[](int idx) &{return *(data_ + idx);}
const_reference operator[](int idx) const&{return *(data_ + idx);}
move_reference operator[](int idx) &&{return *(data_ + idx);}
};
For that matter, this actually generalizes to iterator
-> const_iterator
-> move_iterator
, which looks like an explosion of types and overloads for a simple container.
One could move from elements in a container:
void f(vector<ExpensiveType>&& v){
ExpensiveType et = std::move(v)[0]; // v[0] is might moved here
}
I know that I could do et = std::move(v[0])
in this example, but std::move(v)[0]
is more generic because one can let the container handle the move in a more specific way (via the operator[]&&
).
Has something like this been discussed or implemented in some container-like class?
@Praetorian suggests to return a value from the rvalue-ref qualified overload.
value_type operator[](int idx) &&{return std::move??(*(data_ + idx));}