title | description | ms.date | f1_keywords | helpviewer_keywords | dev_langs | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
empty_view class (C++ Standard Library) |
API reference for the Standard Template Library (STL) <ranges> empty_view class, which is a view that has 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. |
10/18/2022 |
|
|
|
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.
template<class T>
requires std::is_object_v<T>
class empty_view : public ranges::view_interface<empty_view<T>>;
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.
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 |
Member functions | Description |
---|---|
ConstructorsC++20 | Construct an empty_view . |
begin C++20 |
Returns nullptr . |
end C++20 |
Returns nullptr . |
size C++20 |
Returns 0 |
Inherited from view_interface |
Description |
back C++20 |
Results in undefined behavior. |
data C++20 |
Returns nullptr . |
empty C++20 |
Returns true . |
front C++20 |
Results in undefined behavior. |
operator[] C++20 |
Results in undefined behavior. |
operator bool C++20 |
Returns false . |
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.
Header: <ranges>
(since C++20)
Namespace: std::ranges
Compiler Option: /std:c++20
or later is required.
Create an instance of a empty_view
.
template<class T>
inline constexpr empty_view<T> empty{};
T
The type of the underlying element, of which there is none.
The best way to create a empty_view
is by using the empty
range adaptor.
// 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
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>;
None.
None.
Calling this function in a debug build raises an assertion that the function has been called on an empty view_interface
.
Returns nullptr
because there isn't a first element in the view.
static constexpr T* begin() noexcept
nullptr
Returns nullptr
because there isn't a first element in the view to get a pointer to.
static constexpr T* data() noexcept
nullptr
.
Test whether the derived view is empty.
static constexpr bool empty() noexcept
None.
Returns true
.
Returns nullptr
because there aren't any elements in the view.
static constexpr T* end() noexcept
nullptr
.
Results in undefined behavior.
constexpr auto front()
requires ranges::forward_range<T>;
constexpr auto front() const
requires ranges::forward_range<const T>;
None.
None.
Calling this function in a debug build raises an assertion that the function has been called on an empty view_interface
.
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;
pos
The position, relative to the beginning iterator, of the element to return.
None.
Calling this function in a debug build raises an assertion that index is out of range for view_interface
.
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 ()); };
None.
Returns false
.
// 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
Get the number of elements in the view, which will always be 0.
static constexpr size_t size()
None.
0
.