Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::cbegin return the same type as std::begin

cppreference shows this signature for std::cbegin:

template< class C >
constexpr auto cbegin( const C& c ) -> decltype(std::begin(c));

Shouldn't it return something like C::const_iterator instead?

like image 702
qdii Avatar asked Apr 07 '15 07:04

qdii


1 Answers

c is a const reference, so std::begin(c) it will return whatever the const overload of C::begin() returns. For standard library types, this is a const_iterator. For an array type, it is a pointer to const.

Note that this relies on other, non-standard library user defined C, being implemented sanely with a const overload for C::begin() that returns an iterator that gives you const access to the container's elements.

like image 149
juanchopanza Avatar answered Sep 25 '22 03:09

juanchopanza