Namespaces
Variants
Views
Actions

std::ranges::begin, std::ranges::cbegin

From cppreference.com
< cpp‎ | ranges
Revision as of 09:34, 27 June 2020 by T. Canens (Talk | contribs)

 
 
Ranges library
Range adaptors
 
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.

range-begin-end.svg

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, then ranges::begin(t) is ill-formed.
  • If t is a raw array and std::remove_all_extents_t<T> is incomplete, then ranges::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 to t.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

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) [edit]