|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
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 */ | (since C++20) | |
Returns a sentinel indicating the end of a range.
If the argument is an lvalue orranges::enable_borrowed_range<std::remove_cv_t<T>> istrue, then a call toranges::end
isexpression-equivalent to:
ranges::end
is ill-formed,no diagnostic required.T
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 |
The nameranges::end
denotes acustomization point object, which is a constfunction object of aliteralsemiregular
class type. SeeCustomizationPointObject for details.
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.
#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!
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P2602R2 | C++20 | there's machinery to prohibit certain non-memberend found byADL | removed such machinery |
(C++20) | returns a sentinel indicating the end of a read-only range (customization point object)[edit] |
(C++20) | 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] |