Defined in header
inline constexpr /*unspecified*/ counted = /*unspecified*/;
template< class Iterator, class DifferenceType >
requires /* see below */
A counted view presents a
view of the elements of the counted range
) for some iterator
i and non-negative integer
A counted range
) is the
n elements starting with the element pointed to by
i and up to but not including the element, if any, pointed to by the result of
n applications of ++i.
If n == 0, the counted range is valid and empty. Otherwise, the counted range is only valid if
n is positive,
i is dereferenceable, and
) is a valid counted range.
input_or_output_iteratorand decltype((count)) models std::convertible_to<D>,
contiguous_iterator, then views::counted(it, count) is expression-equivalent to std::span(std::to_address(it), static_cast<std::size_t>(static_cast<D>(count))),
- otherwise, if
random_access_iterator, then views::counted(it, count) is expression-equivalent to ranges::subrange(it, it + static_cast<D>(count)),
- otherwise, views::counted(it, count) is expression-equivalent to ranges::subrange(std::counted_iterator(it, count), std::default_sentinel).
- Otherwise, views::counted(it, count) is ill-formed.
Customization point objects
views::counted 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
All instances of
__counted_fn are equal. The effects of invoking different instances of type
__counted_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,
views::counted 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
- std::invocable<__counted_fn, Args...>,
- std::invocable<const __counted_fn, Args...>,
- std::invocable<__counted_fn&, Args...>, and
- std::invocable<const __counted_fn&, Args...>.
Otherwise, no function call operator of
__counted_fn participates in overload resolution.
1 2 3 2 3 4
 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
|DR||Applied to||Behavior as published||Correct behavior|
|P2393R1||C++20||implicit conversion from an integer-class type to std::size_t might be invalid||made explicit|
 See also
| a |
(class template) (range adaptor object)
| combines an iterator-sentinel pair into a |
| iterator adaptor that tracks the distance to the end of the range |
| returns the number of elements satisfying specific criteria|