The goal of cbegin / cend is to solve a specific problem. Consider this code:
std::vector<int> & v =
The problem is that, because of how the template output rules work, the compiler cannot output the template iterator argument to statement (1), because Container::iterator and Container::const_iterator are (potentially) two completely unrelated types (even if the former is implicitly convertible in the latter).
Statement (2) is not exactly a beautiful line, so we need cend() .
Now front() , back() et similia all return the link. A non-constant reference can always be inferred as const in a template function, i.e.
template<class T> void f( const T & l, const T & r); int main() { int x; vector<int> v;
Since Container::const_reference requires that the standard be equal to const Container::value_type & , cfront() / cback() do not buy anything for us.
It's worth noting that another container library (looking at you Qt) is implemented using Copy-On-Write.
This means that calling the const function in such a container is potentially much cheaper than calling the equivalent non-envelope version, simply because a non-constant can copy the entire container under the hood.
For this reason, there are many constFunction in their interface in Qt containers, and the user has the right to choose the right one.
source share