empty_view class (C++ Standard Library)

A view with no elements. This view is useful for test purposes such as calling code that needs to be provided with a view but doesn't need to access its underlying data.

Syntax

template<class T>
    requires std::is_object_v<T>
class empty_view : public ranges::view_interface<empty_view<T>>;

Template parameters

T
The type of the element. Even though there are no elements in an empty_view, all ranges are homogeneous. That is, they have elements of a particular type. So even though an empty_view has no elements, it still has a type, such as an empty_view of int, or strings, etc.

View characteristics

For a description of the following entries, see View class characteristics

Characteristic Description
Range adaptor views::empty
Underlying range None
Element type As specified when the empty_view is created
View iterator category contiguous_range
Sized Yes. Always returns 0
Is const-iterable Yes
Common range Yes
Borrowed range Yes

Members

Member functions Description
ConstructorsC++20 Construct an empty_view.
begin C++20 Returns nullptr.
endC++20 Returns nullptr.
sizeC++20 Returns 0
Inherited from view_interface Description
backC++20 Results in undefined behavior.
dataC++20 Returns nullptr.
emptyC++20 Returns true.
frontC++20 Results in undefined behavior.
operator[]C++20 Results in undefined behavior.
operator boolC++20 Returns false.

Remarks

The best way to create a empty_view is by using the empty range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.

Because there can never be any elements in an empty_view, certain compiler optimizations are possible. For example, the compiler will eliminate for (auto e : std::views::empty<int>) {...} because it knows that there's nothing to iterate over.

Another use for empty_view is splitting a split_view with an empty_view delimiter, which results in a range of single element ranges.

Requirements

Header: <ranges> (since C++20)

Namespace: std::ranges

Compiler Option: /std:c++20 or later is required.

Constructors

Create an instance of a empty_view.

template<class T>
inline constexpr empty_view<T> empty{};

Parameters

T
The type of the underlying element, of which there is none.

Remarks

The best way to create a empty_view is by using the empty range adaptor.

Example empty_view

// requires /std:c++20 or higher
#include <ranges>
#include <iostream>

int main()
{
    auto anEmptyView = std::views::empty<int>;
    bool isNotEmpty = (bool)anEmptyView;
    std::cout << std::boolalpha << isNotEmpty << "\n"; // false
    std::cout << std::boolalpha << anEmptyView.empty(); // true
}
false
true

back

Results in undefined behavior.

constexpr auto back()
    requires ranges::bidirectional_range<T> && ranges::common_range<T>;

constexpr auto back() const
    requires ranges::bidirectional_range<const T> && ranges::common_range<const T>;

Parameters

None.

Return value

None.

Remarks

Calling this function in a debug build raises an assertion that the function has been called on an empty view_interface.

begin

Returns nullptr because there isn't a first element in the view.

static constexpr T* begin() noexcept

Return value

nullptr

data

Returns nullptr because there isn't a first element in the view to get a pointer to.

static constexpr T* data() noexcept

Return value

nullptr.

empty

Test whether the derived view is empty.

static constexpr bool empty() noexcept

Parameters

None.

Return value

Returns true.

end

Returns nullptr because there aren't any elements in the view.

static constexpr T* end() noexcept

Return value

nullptr.

front

Results in undefined behavior.

constexpr auto front()
    requires ranges::forward_range<T>;
constexpr auto front() const
    requires ranges::forward_range<const T>;

Parameters

None.

Return value

None.

Remarks

Calling this function in a debug build raises an assertion that the function has been called on an empty view_interface.

operator[]

Results in undefined behavior.

template<ranges::random_access_range R = T>
constexpr decltype(auto) operator[](ranges::range_difference_t<R> pos);

template<ranges::random_access_range R = const T>
constexpr decltype(auto) operator[](ranges::range_difference_t<R> pos) const;

Parameters

pos
The position, relative to the beginning iterator, of the element to return.

Return value

None.

Remarks

Calling this function in a debug build raises an assertion that index is out of range for view_interface.

operator bool

Test whether the derived view isn't empty.

constexpr explicit operator bool()
requires requires { ranges::empty(T ()); };

constexpr explicit operator bool() const
requires requires { ranges::empty(T ()); };

Parameters

None.

Return value

Returns false.

Example (bool)

// requires /std:c++20 or higher
#include <ranges>
#include <iostream>

int main()
{
    auto anEmptyView = std::views::empty<int>;

    if (anEmptyView) // check if anEmptyView isn't empty
    {
        std::cout << "Error: why does an empty_view have elements?\n";
    }
    else
    {
        std::cout << "Correct: an empty_view is not not empty\n";
    }
}
Correct: an empty_view is not not empty

size

Get the number of elements in the view, which will always be 0.

static constexpr size_t size()

Parameters

None.

Return value

0.

See also

<ranges>
empty range adaptor
single_view
view classes