Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::rbegin

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

         inlineconstexpr/* unspecified */ rbegin=/* unspecified */;

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

          requires/* see below */

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

      Returns an iterator to the last element of the argument.

      range-rbegin-rend.svg

      IfT is an array type andstd::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then the call toranges::rbegin is ill-formed, no diagnostic required.

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

      1. decay-copy(t.rbegin())(until C++23)auto(t.rbegin())(since C++23), if that expression is valid and its type modelsstd::input_or_output_iterator.
      2. Otherwise,decay-copy(rbegin(t))(until C++23)auto(rbegin(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 ofrbegin is established as if by performingargument-dependent lookup only.
      3. Otherwise,std::make_reverse_iterator(ranges::end(t)) if bothranges::begin(t) andranges::end(t) are valid expressions, have the same type, and that type modelsstd::bidirectional_iterator.

      In all other cases, a call toranges::rbegin is ill-formed, which can result insubstitution failure whenranges::rbegin(t) appears in the immediate context of a template instantiation.

      Contents

      Customization point objects

      The nameranges::rbegin 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::rbegin 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 underlyingrbegin 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 <iostream>#include <ranges>#include <span>#include <vector> int main(){std::vector<int> v={3,1,4};auto vi= std::ranges::rbegin(v);std::cout<<*vi<<'\n';*vi=42;// OK int a[]={-5,10,15};auto ai= std::ranges::rbegin(a);std::cout<<*ai<<'\n';*ai=42;// OK // auto x_x = std::ranges::rbegin(std::vector{6, 6, 6});// ill-formed: the argument is an rvalue (see Notes ↑) auto si= std::ranges::rbegin(std::span{a});// OK    static_assert(std::ranges::enable_borrowed_range<std::remove_cv_t<decltype(std::span{a})>>);*si=42;// OK}

      Output:

      415

      [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-memberrbegin found byADLremoved such machinery

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp