Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::begin

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      Defined in header<iterator>
      inlinenamespace/* unspecified */{

         inlineconstexpr/* unspecified */ begin=/* unspecified */;

      }
      (since C++20)
      (customization point object)
      Call signature
      template<class T>

          requires/* see below */

      constexprstd::input_or_output_iteratorauto begin( T&& t);
      (since C++20)

      Returns an iterator to the first element of the argument.

      range-begin-end.svg

      If the argument is an lvalue orranges::enable_borrowed_range<std::remove_cv_t<T>> istrue, then a call toranges::begin isexpression-equivalent to:

      1. t+0 ift has an array type.
      2. Otherwise,decay-copy(t.begin())(until C++23)auto(t.begin())(since C++23), if that expression is valid and its type modelsstd::input_or_output_iterator.
      3. Otherwise,decay-copy(begin(t))(until C++23)auto(begin(t))(since C++23), ifT is a class or enumeration type, that expression is valid and its type modelsstd::input_or_output_iterator, where the meaning ofbegin is established as if by performingargument-dependent lookup only.

      In all other cases, a call toranges::begin is ill-formed, which can result insubstitution failure when the call appears in the immediate context of a template instantiation.

      Contents

      Customization point objects

      The nameranges::begin denotes acustomization point object, which is a constfunction object of aliteralsemiregular class type. SeeCustomizationPointObject for details.

      [edit]Notes

      If the argument is an rvalue (i.e.T is an object type) andranges::enable_borrowed_range<std::remove_cv_t<T>> isfalse, the call toranges::begin is ill-formed, which also results in substitution failure.

      The return type modelsstd::input_or_output_iterator in all cases.

      The C++20 standard requires that if the underlyingbegin function call returns a prvalue, the return value is move-constructed from the materialized temporary object. All implementations directly return the prvalue instead. The requirement is corrected by the post-C++20 proposalP0849R8 to match the implementations.

      [edit]Example

      Run this code
      #include <cassert>#include <ranges>#include <vector> int main(){std::vector v{3,1,4};auto vi= std::ranges::begin(v);auto vci= std::ranges::cbegin(v);assert(*vi==3 and*vi==*vci);++vi;++vci;// OK: vci is modifiable object*vi=42;// OK: vi points to mutable element// *vci = 13; // Error: vci points to immutable element int a[]{-5,10,15};auto ai= std::ranges::begin(a);// works with C-arrays as wellassert(*ai==-5);*ai=42;// OK}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      P2602R2C++20there's machinery to prohibit certain non-memberbegin found byADLremoved such machinery

      [edit]See also

      returns an iterator to the beginning of a read-only range
      (customization point object)[edit]
      (C++11)(C++14)
      returns an iterator to the beginning of a container or array
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/begin&oldid=158238"

      [8]ページ先頭

      ©2009-2025 Movatter.jp