Namespaces
Variants
Views
Actions

std::ranges::ssize

From cppreference.com
< cpp‎ | ranges
 
 
 
Defined in header <ranges>
inline namespace /*unspecified*/ {

    inline constexpr /*unspecified*/ ssize = /*unspecified*/;

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

    requires /* see below */

constexpr /*signed-integral-like*/ auto ssize(T&& t);


Let t be an object of type T.

ranges::ssize is expression-equivalent to:

1) static_cast<std::ptrdiff_t>(ranges::size(std::forward<T>(t))) if sizeof(ranges::range_difference_t<T>) < sizeof(std::ptrdiff_t).
2) static_cast<ranges::range_difference_t>(ranges::size(std::forward<T>(t))) if t otherwise.

Contents

[edit] Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

[edit] Customization point objects

The name ranges::ssize denotes a customization point object, which is a const function object of a literal semiregular class type (denoted, for exposition purposes, as ssize_ftor). All instances of ssize_ftor are equal. Thus, ranges::ssize 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::ssize above, ssize_ftor will satisfy std::invocable<const ssize_ftor&, Args...>. Otherwise, no function call operator of ssize_ftor participates in overload resolution.

[edit] Example

#include <array>
#include <iostream>
#include <ranges>
#include <type_traits>
 
int main()
{
    std::array arr{1, 2, 3, 4, 5};
    auto s = std::ranges::ssize(arr);
 
    std::cout << "ranges::ssize(arr) = " << s << '\n'
              << "ranges::ssize is "
              << (std::is_signed_v<decltype(s)> ? "signed" : "unsigned")
              << '\n';
 
    std::cout << "reversed arr: ";
 
    for (--s; s >= 0; --s)
        std::cout << arr[s] << ' ';
 
    std::cout << "\n" "s = " << s << '\n';
}

Output:

ranges::ssize(arr) = 5
ranges::ssize is signed
reversed arr: 5 4 3 2 1 
s = -1

[edit] See also

obtains the size of a range whose size can be calculated in constant time
(customization point object) [edit]
(C++17)(C++20)
returns the size of a container or array
(function template) [edit]