std::ranges::size

From cppreference.com
< cpp‎ | ranges
 
 
Ranges library
Range access
Range conversions
(C++23)
Range primitives



Dangling iterator handling
Range concepts
Views

Factories






Adaptors
Range adaptor objects
Range adaptor closure objects
Helper items
 
Defined in header <ranges>
inline namespace /*unspecified*/ {

    inline constexpr auto size = /*unspecified*/;

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

    requires /* see below */

constexpr auto size( T&& t );
(since C++20)

Calculates the number of elements in t in constant time.

Let t be an object of type T. A call to ranges::size is expression-equivalent to:

  1. std::extent_v<T>, if T is an array type with a known bound.
  2. Otherwise, t.size() converted to its decayed type, if ranges::disable_sized_range<std::remove_cv_t<T>> is false, and the converted expression is valid and has an integer-like type.
  3. Otherwise, size(t) converted to its decayed type, if ranges::disable_sized_range<std::remove_cv_t<T>> is false, and the converted expression is valid and has an integer-like type, where the overload resolution is performed with the following candidates:
    • void size(auto&) = delete;
    • void size(const auto&) = delete;
  4. Otherwise, /*to-unsigned-like*/(ranges::end(t) - ranges::begin(t)), if T models ranges::forward_range and ranges::sentinel_t<T> models std::sized_sentinel_for<ranges::iterator_t<T>>,
    where /*to-unsigned-like*/ denotes an explicit conversion to an unsigned-integer-like type.

In all other cases, a call to ranges::size is ill-formed, which can result in substitution failure when ranges::size(t) appears in the immediate context of a template instantiation.

Expression-equivalent

Expression e is expression-equivalent to expression f, if

  • e and f have the same effects, and
  • either both are constant subexpressions or else neither is a constant subexpression, and
  • either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).

Customization point objects

The name ranges::size denotes a customization point object, which is a const function object of a literal semiregular class type. For exposition purposes, the cv-unqualified version of its type is denoted as __size_fn.

All instances of __size_fn are equal. The effects of invoking different instances of type __size_fn on the same arguments are equivalent, regardless of whether the expression denoting the instance is an lvalue or rvalue, and is const-qualified or not (however, a volatile-qualified instance is not required to be invocable). Thus, ranges::size can be copied freely and its copies can be used interchangeably.

Given a set of types Args..., if std::declval<Args>()... meet the requirements for arguments to ranges::size above, __size_fn models

Otherwise, no function call operator of __size_fn participates in overload resolution.

Notes

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.

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::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';
 
    std::cout << std::boolalpha << "is_signed: "
              << std::is_signed_v<decltype(std::ranges::size(v))> << '\n';
}

Output:

ranges::size(v) == 0
ranges::size(il) == 1
ranges::size(array) == 2
is_signed: false

See also

returns a signed integer equal to the size of a range
(customization point object)
specifies that a range knows its size in constant time
(concept)
(C++17)(C++20)
returns the size of a container or array
(function template)