std::ranges::size
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 */ |
||
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:
- std::extent_v<T>, if
T
is an array type with a known bound. - Otherwise, std::forward<T>(t).size(), if not ranges::disable_sized_range<std::remove_cv_t<T>>, and std::forward<T>(t).size() is valid and returns a type that is /*integer-like*/.
- where /*integer-like*/ is a type that models std::integral or is a class that behaves like an integer type, including all operators, implicit conversions, and std::numeric_limits specializations.
- Otherwise, size(std::forward<T>(t)), if not ranges::disable_sized_range<std::remove_cv_t<T>>, and size(std::forward<T>(t)) is valid and returns a type that is /*integer-like*/, where the overload resolution is performed with the following candidates:
- void begin(auto&) = delete;
- void begin(const auto&) = delete;
- 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 /*integer-like*/ type that behaves like an unsigned integer type, including all operators, implicit conversions, and std::numeric_limits specializations.
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, 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.
Customization point objects
The name ranges::size
denotes a customization point object, which is a const function object of a literal semiregular class type (denoted, for exposition purposes, as size_ftor
). All instances of size_ftor
are equal. 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_ftor
will satisfy std::invocable<const size_ftor&, Args...>. Otherwise, no function call operator of size_ftor
participates in overload resolution.
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
(C++20) |
obtains the size of a range whose size can be calculated in constant time and converts it to signed integral (customization point object) |
(C++17)(C++20) |
returns the size of a container or array (function template) |