inline constexpr auto size = /* unspecified */;
Call signature
template< class T > requires /* see below */
Calculates the number of elements in t in constant time.
Given the subexpression of which t denotes the (possibly materialized) result object as E, and the type of E as T
:
T
is an array of unknown bound, ranges::size(E) is ill-formed.T
is an array type, ranges::size(E) is expression-equivalent to decay-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 performing argument-dependent lookup only.to-unsigned-like
(ranges::end(t) - ranges::begin(t)):
T
models forward_range
.I
and the type of ranges::end(t) as S
, both sized_sentinel_for
<S, I> and forward_iterator
<I> are modeled.to-unsigned-like
(ranges::end(t) - ranges::begin(t)) is a valid expression.Diagnosable ill-formed cases above result in substitution failure when ranges::size(E) appears in the immediate context of a template instantiation.
Customization point objectsThe name ranges::size
denotes a customization point object, which is a const function object of a literal semiregular
class type. See CustomizationPointObject for details.
Whenever ranges::size(e) is valid for an expression e, the return type is integer-like.
The C++20 standard requires that if the underlying size
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 proposal P0849R8 to match the implementations.
The expression ranges::distance(e) can also be used to determine the size of a range e. Unlike ranges::size(e), ranges::distance(e) works even if e is an unsized range, at the cost of having linear complexity in that case.
[edit] Example#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_list std::cout << "ranges::size(il) == " << std::ranges::size(il) << '\n'; int array[]{4, 5}; // array has a known bound std::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) == 0 ranges::size(il) == 1 ranges::size(array) == 2[edit] Defect reports
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 by ADL removed such machinery [edit] See also returns a signed integer equal to the size of a range
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4