Spec-Zone.ru › C++

std::ranges::view_interface<D>::back

constexpr decltype(auto) back()
    requires ranges::bidirectional_range<D> && ranges::common_range<D>;
(1) (since C++20)
constexpr decltype(auto) back() const
    requires ranges::bidirectional_range<const D> && ranges::common_range<const D>;
(2) (since C++20)

The default implementation of back() member function returns the last element in the view of the derived type. Whether the element is returned by value or by reference depends on the operator* of the iterator type.

Equivalent to return *ranges::prev(ranges::end(derived));, where derived is:

1) static_cast<D&>(*this)
2) static_cast<const D&>(*this)

If empty() is true, the behavior is undefined.

(until C++26)

If empty() is true:

  • If the implementation is hardened, a contract violation occurs.
  • If the implementation is not hardened, the behavior is undefined.
(since C++26)

Return value

The last element in the view.

Notes

In C++20, no type derived from std::ranges::view_interface in the standard library provides their own back() member function.

However, following derived types cannot use the default implementations, as they never satisfy neither bidirectional_range nor common_range:

  • std::ranges::basic_istream_view
  • std::ranges::lazy_split_view
  • std::ranges::split_view
  • std::ranges::take_while_view

The inherited back() member function is available for std::ranges::empty_view, but a call to it always results in undefined behavior.

Example

See also

rbegincrbegin
(C++14)
returns a reverse iterator to the beginning of a container or array
(function template)
ranges::rbegin
(C++20)
returns a reverse iterator to a range
(customization point object)
ranges::crbegin
(C++20)
returns a reverse iterator to a read-only range
(customization point object)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/ranges/view_interface/back

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API