std::ranges::ssize
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Ranges library
(customization point object)
returns the distance between an iterator and a sentinel, or between the beginning and end of a range
(algorithm function object)[edit]
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++26)(C++26)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)
Defined in header
<ranges>
Defined in header
<iterator>
inline namespace /* unspecified */ {
(since C++20) inline constexpr /* unspecified */ ssize = /* unspecified */;
(customization point object)
Call signature
template< class T >
(since C++20)
requires /* see below */
Calculates the number of elements in t in constant time, and converts the result to a signed type.
Given the subexpression of which t denotes the (possibly materialized) result object as E:
- If ranges::size (t) is ill-formed, ranges::ssize(E) is also ill-formed.
- Otherwise, let
Signed
bemake-signed-like-t
<decltype(ranges::size (t))>:- If std::ptrdiff_t is wider than
Signed
, ranges::ssize(E) is expression-equivalent to static_cast<std::ptrdiff_t >(ranges::size (t)). - Otherwise, ranges::ssize(E) is expression-equivalent to static_cast<Signed>(ranges::size (t)).
- If std::ptrdiff_t is wider than
Customization point objects
The name ranges::ssize
denotes a customization point object, which is a const function object of a literal semiregular
class type. See CustomizationPointObject for details.
[edit] Notes
If ranges::ssize(e) is valid for an expression e, the return type is a signed-integer-like type.
[edit] Example
Run this code
#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] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3403 | C++20 | ranges::size worked for some non-range types but ranges::ssize did not
|
made work |
[edit] See also
(C++20)
(algorithm function object)[edit]