|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
Defined in header <iterator> | ||
inlinenamespace/* unspecified */{ inlineconstexprauto size=/* unspecified */; | (since C++20) (customization point object) | |
Call signature | ||
template<class T> requires/* see below */ | (since C++20) | |
Calculates the number of elements int in constant time.
Given thesubexpression of whicht denotes the (possiblymaterialized) result object asE, and the type ofE asT
:
T
is an array of unknown bound,ranges::size(E) is ill-formed.T
is an array type,ranges::size(E) isexpression-equivalent todecay-copy (std::extent_v<T>)(until C++23)auto(std::extent_v<T>)(since C++23).T
is a class or enumeration type.size
is established as if by performingargument-dependent lookup only.to-unsigned-like
(ranges::end(t)-ranges::begin(t)):T
modelsforward_range
.I
and the type ofranges::end(t) asS
, bothsized_sentinel_for
<S, I> andforward_iterator
<I> are modeled.to-unsigned-like
(ranges::end(t)-ranges::begin(t)) is a valid expression.Diagnosable ill-formed cases above result insubstitution failure whenranges::size(E) appears in the immediate context of a template instantiation.
Contents |
The nameranges::size
denotes acustomization point object, which is a constfunction object of aliteralsemiregular
class type. SeeCustomizationPointObject for details.
Wheneverranges::size(e) is valid for an expressione, the return type isinteger-like.
The C++20 standard requires that if the underlyingsize
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.
The expressionranges::distance(e) can also be used to determine the size of a rangee. Unlikeranges::size(e),ranges::distance(e) works even ife is an unsized range, at the cost of having linear complexity in that case.
#include <iostream>#include <ranges>#include <type_traits>#include <vector> int main(){auto v=std::vector<int>{};std::cout<<"ranges::size(v) == "<< std::ranges::size(v)<<'\n'; auto il={7};// std::initializer_liststd::cout<<"ranges::size(il) == "<< std::ranges::size(il)<<'\n'; int array[]{4,5};// array has a known boundstd::cout<<"ranges::size(array) == "<< std::ranges::size(array)<<'\n'; static_assert(std::is_signed_v<decltype(std::ranges::size(v))>==false);}
Output:
ranges::size(v) == 0ranges::size(il) == 1ranges::size(array) == 2
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-membersize found byADL | removed such machinery |
(C++20) | returns a signed integer equal to the size of a range (customization point object)[edit] |
(C++20) | specifies that a range knows its size in constant time (concept)[edit] |
(C++20) | returns the distance between an iterator and a sentinel, or between the beginning and end of a range (algorithm function object)[edit] |
(C++17)(C++20) | returns the size of a container or array (function template)[edit] |