Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::end

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

         inlineconstexpr/* unspecified */ end=/* unspecified */;

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

          requires/* see below */

      constexprstd::sentinel_for<ranges::iterator_t<T>>auto end( T&& t);
      (since C++20)

      Returns a sentinel indicating the end of a range.

      range-begin-end.svg

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

      1. t+std::extent_v<T> ift has an array type of known bound.
      2. Otherwise,decay-copy(t.end())(until C++23)auto(t.end())(since C++23), if that expression is valid, and its type modelsstd::sentinel_for<ranges::iterator_t<T>>.
      3. Otherwise,decay-copy(end(t))(until C++23)auto(end(t))(since C++23), ifT is a class or enumeration type, that expression is valid and its converted type modelsstd::sentinel_for<ranges::iterator_t<T>>, where the meaning ofend is established as if by performingargument-dependent lookup only.

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

      Contents

      Customization point objects

      The nameranges::end 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, or if it is of an array type of unknown bound, the call toranges::end is ill-formed, which also results in substitution failure.

      Ifranges::end(std::forward<T>(t)) is valid, thendecltype(ranges::end(std::forward<T>(t))) anddecltype(ranges::begin(std::forward<T>(t))) modelstd::sentinel_for in all cases, whileT modelsstd::ranges::range.

      The C++20 standard requires that if the underlyingend 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 <algorithm>#include <iostream>#include <ranges>#include <vector> int main(){std::vector<int> vec{3,1,4};if(std::ranges::find(vec,5)!= std::ranges::end(vec))std::cout<<"found a 5 in vector vec!\n"; int arr[]{5,10,15};if(std::ranges::find(arr,5)!= std::ranges::end(arr))std::cout<<"found a 5 in array arr!\n";}

      Output:

      found a 5 in array arr!

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

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp