Skip to content

Search topics & cppreference

Search all study topics, concept maps, and 6,587 C++ and C reference pages.

std::ranges::view_interface<D>::size

The default implementation of size() member function obtains the size of the range by calculating the difference between the sentinel and the beginning iterator.

Ranges
  • C++17
  • C++20
1 min readView on cppreference
NotebookLM
constexpr auto size() requires ranges::forward_range<D> &&
    std::sized_sentinel_for<ranges::sentinel_t<D>,
                            ranges::iterator_t<D>>;
(1) (since C++20)
constexpr auto size() const requires ranges::forward_range<const D> &&
    std::sized_sentinel_for<ranges::sentinel_t<const D>,
                            ranges::iterator_t<const D>>;
(2) (since C++20)

The default implementation of size() member function obtains the size of the range by calculating the difference between the sentinel and the beginning iterator.

Return value

1) to-unsigned-like (ranges::end(static_cast<D&>(this)) -
                     ranges::begin(static_cast<D&>(this)))
.
2) to-unsigned-like (ranges::end(static_cast<const D&>(this)) -
                     ranges::begin(static_cast<const D&>(this)))
.

Notes

Following derived types may use the default implementation of size():

Following types are derived from std::ranges::view_interface and do not declare their own size() member function, but they cannot use the default implementation, because their iterator and sentinel types never satisfy sized_sentinel_for:

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 3646 C++20 the default implementations of size functions returned a signed type they return unsigned type

See also

(C++17)(C++20)
returns the size of a container or array
(function template)
returns an integer equal to the size of a range
(customization point object)
returns a signed integer equal to the size of a range
(customization point object)

Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.