std::ranges::cbegin

Header: <ranges>

const std::remove_reference_t& if the argument is an lvalue (i.e. T is an lvalue reference type),

# Declarations

inline namespace /* unspecified */ {
inline constexpr /* unspecified */ cbegin = /* unspecified */;
}

(since C++20) (customization point object)

Call signature
template< class T >
requires /* see below */
constexpr /* see below */ auto cbegin( T&& t );

(since C++20)

# Notes

For an lvalue range e of type T, ranges::cbegin(e) is equivalent to

ranges::begin(std::as_const(e)).

# Example

#include <cassert>
#include <ranges>
#include <vector>
 
int main()
{
    std::vector v{3, 1, 4};
    auto vi = std::ranges::cbegin(v);
    assert(3 == *vi);
    ++vi; // OK, constant-iterator object is mutable
    assert(1 == *vi);
    // *vi = 13; // Error: constant-iterator points to an immutable element
 
    int a[]{3, 1, 4};
    auto ai = std::ranges::cbegin(a); // cbegin works with C-arrays as well
    assert(3 == *ai and *(ai + 1) == 1);
    // *ai = 13; // Error: read-only variable is not assignable
}

# See also