std::ranges::ssize

Header: <ranges>

Calculates the number of elements in t in constant time, and converts the result to a signed type.

# Declarations

inline namespace /* unspecified */ {
inline constexpr /* unspecified */ ssize = /* unspecified */;
}

(since C++20) (customization point object)

Call signature
template< class T >
requires /* see below */
constexpr auto ssize( T&& t );

(since C++20)

# Notes

If ranges::ssize(e) is valid for an expression e, the return type is a signed-integer-like type.

# 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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3403C++20ranges::size worked for some non-range types but ranges::ssize did notmade work

# See also