From cppreference.com
Defined in header
Defined in header
inlinenamespace/* unspecified */{inlineconstexprautosize=/* unspecified */;}(since C++20)
(customization point object)Call signature
template<classT>requires/* see below */constexprautosize(T&&t);(since C++20)Calculates the number of elements in t in constant time.
Given the
of which t denotes the (possibly
) result object as E, and the type of E as T:
If T is an array of unknown bound, ranges::size(E) is ill-formed.
Otherwise, if T is an array type, ranges::size(E) is
to
(std::extent_v<T>)(until C++23)auto(std::extent_v<T>)(since C++23).
Otherwise, if all following conditions are satisfied, ranges::size(E) is expression-equivalent to
(t.size())(until C++23)auto(t.size())(since C++23): ranges::disable_sized_range<std::remove_cv_t<T>> is false.
(t.size())(until C++23)auto(t.size())(since C++23) is a valid expression of
.
Otherwise, if all following conditions are satisfied, ranges::size(E) is expression-equivalent to
(size(t))(until C++23)auto(size(t))(since C++23): T is a class or enumeration type.
ranges::disable_sized_range<std::remove_cv_t<T>> is false.
(size(t))(until C++23)auto(size(t))(since C++23) is a valid expression of integer-like type, where the meaning of size is established as if by performing
only.
Otherwise, if all following conditions are satisfied, ranges::size(E) is expression-equivalent to
(ranges::end(t)-ranges::begin(t)): T models
.
Given the type of ranges::begin(t) as I and the type of ranges::end(t) as S, both
<S,I> and
<I> are modeled.
(ranges::end(t)-ranges::begin(t)) is a valid expression.
Otherwise, ranges::size(E) is ill-formed.
Diagnosable ill-formed cases above result in
when ranges::size(E) appears in the immediate context of a template instantiation.
Customization point objects
The name ranges::size denotes a customization point object, which is a const
of a
class type. See
for details.
Notes
Whenever ranges::size(e) is valid for an expression e, the return type is
.
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
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.
Example
Run this code
#include<iostream>#include<ranges>#include<type_traits>#include<vector>intmain(){autov=std::vector<int>{};std::cout<<"ranges::size(v) == "<<std::ranges::size(v)<<'\n';autoil={7};// std::initializer_liststd::cout<<"ranges::size(il) == "<<std::ranges::size(il)<<'\n';intarray[]{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) == 0 ranges::size(il) == 1 ranges::size(array) == 2 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++20 there's machinery to prohibit certain non-member size found by
removed such machinery See also
(C++20)
returns a signed integer equal to the size of a range
(customization point object)
(C++20)
specifies that a range knows its size in constant time
(concept)
(C++20)
returns the distance between an iterator and a sentinel, or between the beginning and end of a range
(algorithm function object)
(C++17)(C++20)
returns the size of a container or array
(function template)