std::ranges::begin, std::ranges::cbegin
Defined in header <ranges>
|
||
Defined in header <iterator>
|
||
inline constexpr /*unspecified*/ begin = /*unspecified*/; |
(1) | (since C++20) |
inline constexpr /*unspecified*/ cbegin = /*unspecified*/; |
(2) | (since C++20) |
Returns an object that models std::input_or_output_iterator
when applied to a range.
Contents |
Returns
Let t
be an object of type T
. ranges::begin(t)
is resolved as one of the following, in order of preference.
- If t is an rvalue and
ranges::enabled_borrowed_range<T> == false
, thenranges::begin(t)
is ill-formed. - If t is a raw array and
std::remove_all_extents_t<T>
is incomplete, thenranges::begin(t)
is ill-formed, no diagnostic required. - If t is a raw array otherwise, then
ranges::begin(t)
is equivalent to t + 0. - If t.begin() is a valid expression, and its return type models
std::input_or_output_iterator
,ranges::begin(t)
is equivalent tot.begin()
. - If T is a class or enumeration type and the unqualified call begin(t) is a valid expression when return type models
std::input_or_output_iterator
,ranges::begin(t)
is equivalent to begin(t). -
ranges::begin(t)
is otherwise ill-formed.
Customization point objects
The name ranges::begin
denotes a customization point object, which is a const function object of a literal semiregular
class type. For exposition purposes, the cv-unqualified version of its type is denoted as __begin_fn
.
All instances of __begin_fn
are equal. The effects of invoking different instances of type __begin_fn
on the same arguments are equivalent, regardless of whether the expression denoting the instance is an lvalue or rvalue, and is const-qualified or not (however, a volatile-qualified instance is not required to be invocable). Thus, ranges::begin
can be copied freely and its copies can be used interchangeably.
Given a set of types Args...
, if std::declval<Args>()... meet the requirements for arguments to ranges::begin
above, __begin_fn
models
- std::invocable<__begin_fn, Args...>,
- std::invocable<const __begin_fn, Args...>,
- std::invocable<__begin_fn&, Args...>, and
- std::invocable<const __begin_fn&, Args...>.
Otherwise, no function call operator of __begin_fn
participates in overload resolution.
ADL poison pill
ranges::begin
also has a "poison pill" property that ensures the correct unqualified call begin(c) happens when T is a class or enumeration type. This is achieved by requiring overload resolution occur in a context where void begin(auto&) = delete; and void begin(const auto&) = delete; are visible to ranges::begin
's call operator. In practice, these deleted overloads are in an inline namespace that is nested within namespace std::ranges
.
See also
(C++11)(C++14) |
returns an iterator to the beginning of a container or array (function template) |