std::ranges::views::empty, std::ranges::empty_view
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
empty_viewviews::empty
(C++23)(C++23)
(C++23)(C++23)
(C++26)(C++26)
(C++23)(C++23)
(C++26)(C++26)
(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)
Defined in header
<ranges>
template<class T>
(1)
(since C++20)
requires std::is_object_v <T>
namespace views {
(2)
(since C++20)
template<class T>
constexpr empty_view<T> empty{};
1) A range factory that produces a
view
of no elements of a particular type.2) Variable template for
empty_view
.Contents
[edit] Member functions
begin
[static]
(public static member function)
end
[static]
(public static member function)
data
[static]
(public static member function)
size
[static]
(public static member function)
empty
[static]
(public static member function)
Inherited from std::ranges::view_interface
(C++23)
(public member function of
std::ranges::view_interface<D>
) [edit]
(C++23)
(public member function of
std::ranges::view_interface<D>
) [edit]
returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
(public member function of
(public member function of
std::ranges::view_interface<D>
) [edit]
returns the first element in the derived view, provided if it satisfies
(public member function of
forward_range
(public member function of
std::ranges::view_interface<D>
) [edit]
returns the last element in the derived view, provided only if it satisfies
(public member function of
bidirectional_range
and common_range
(public member function of
std::ranges::view_interface<D>
) [edit]
returns the
(public member function of
n
th element in the derived view, provided only if it satisfies random_access_range
(public member function of
std::ranges::view_interface<D>
) [edit]
std::ranges::empty_view::begin
static constexpr T* begin() noexcept { return nullptr; }
(since C++20)
empty_view
does not reference any element.
std::ranges::empty_view::end
static constexpr T* end() noexcept { return nullptr; }
(since C++20)
empty_view
does not reference any element.
std::ranges::empty_view::data
static constexpr T* data() noexcept { return nullptr; }
(since C++20)
empty_view
does not reference any element.
std::ranges::empty_view::size
static constexpr std::size_t size() noexcept { return 0; }
(since C++20)
empty_view
is always empty.
std::ranges::empty_view::empty
static constexpr bool empty() noexcept { return true; }
(since C++20)
empty_view
is always empty.
[edit] Helper templates
template<class T>
constexpr bool ranges::enable_borrowed_range <ranges::empty_view<T>> = true;
(since C++20)
constexpr bool ranges::enable_borrowed_range <ranges::empty_view<T>> = true;
This specialization of ranges::enable_borrowed_range makes empty_view
satisfy borrowed_range
.
[edit] Notes
Although empty_view
obtains front
, back
, and operator[] member functions from view_interface
, calls to them always result in undefined behavior since an empty_view
is always empty.
The inherited operator bool conversion function always returns false.
[edit] Example
Run this code
#include <ranges> int main() { namespace ranges = std::ranges; ranges::empty_view<long> e; static_assert(ranges::empty (e)); // uses operator bool static_assert(0 == e.size()); static_assert(nullptr == e.data()); static_assert(nullptr == e.begin()); static_assert(nullptr == e.end()); static_assert(nullptr == e.cbegin()); static_assert(nullptr == e.cend()); }
[edit] See also
(C++20)
view
that contains a single element of a specified value(class template) (customization point object)[edit]