Why element access member functions of STL containers, e.g. std::array::operator[]
or std::vector::operator[]
do not have rvalue ref-qualifier over
There isn't a particular problem with adding ref-qualifiers to everything that returns references, however that basically doubles the number of members, which will generally have identical implementations apart from wrapping the return in std::move
.
class A
{
int a;
int& operator[](std::size_t pos) & { return a; }
int&& operator[](std::size_t pos) && { return std::move(a); }
};
The standard library has declined to provide these overloads, in the same way that it has declined to provide many volatile
overloads. In this case, you can just std::move
the &
value, where you need it.
If you are writing your own containers, then there is no safeness reason to avoid such overloads. It does increase the maintenance burden, so I would advise against it.