Difference between revisions of "cpp/ranges/empty"
(Created page with "{{cpp/ranges/title | empty}} {{cpp/ranges/navbar}} {{dcl begin}} {{dcl header | ranges}} {{dcl header | iterator}} {{dcl | notes={{mark custpt}} | since=c++20 | 1= inline nam...") |
m |
||
Line 14: | Line 14: | ||
template< class T > | template< class T > | ||
requires /* see below */ | requires /* see below */ | ||
− | constexpr bool | + | constexpr bool empty(T&& t); |
}} | }} | ||
{{dcl end}} | {{dcl end}} |
Revision as of 11:11, 3 July 2020
Defined in header <ranges>
|
||
Defined in header <iterator>
|
||
inline namespace /*unspecified*/ { inline constexpr auto empty = /*unspecified*/; |
(since C++20) (customization point object) |
|
Call signature |
||
template< class T > requires /* see below */ |
||
Determines whether or not t
has any elements.
Let t
be an object of type T
. A call to ranges::empty
is expression-equivalent to:
- bool(std::forward<T>(t).empty()), if that expression is valid.
- Otherwise, (ranges::size(std::forward<T>(t)) == 0), if that expression is valid.
- Otherwise, bool(ranges::begin(t) == ranges::end(t))
In all other cases, a call to ranges::empty
is ill-formed, which can result in substitution failure when ranges::empty(t) appears in the immediate context of a template instantiation.
Contents |
Expression-equivalent
Expression e is expression-equivalent to expression f, if
- e and f have the same effects, and
- either both are constant subexpressions or else neither is a constant subexpression, and
- either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).
Customization point objects
The name ranges::empty
denotes a customization point object, which is a const function object of a literal semiregular
class type. For exposition purposes, the cv-unqualified version of its type is denoted as __empty_fn
.
All instances of __empty_fn
are equal. The effects of invoking different instances of type __empty_fn
on the same arguments are equivalent, regardless of whether the expression denoting the instance is an lvalue or rvalue, and is const-qualified or not (however, a volatile-qualified instance is not required to be invocable). Thus, ranges::empty
can be copied freely and its copies can be used interchangeably.
Given a set of types Args...
, if std::declval<Args>()... meet the requirements for arguments to ranges::empty
above, __empty_fn
models
- std::invocable<__empty_fn, Args...>,
- std::invocable<const __empty_fn, Args...>,
- std::invocable<__empty_fn&, Args...>, and
- std::invocable<const __empty_fn&, Args...>.
Otherwise, no function call operator of __empty_fn
participates in overload resolution.
Notes
If the argument is an rvalue (i.e. T
is an object type) and ranges::enable_borrowed_range<std::remove_cv_t<T>> is false, the call to ranges::begin
is ill-formed, which also results in substitution failure.
The return type models std::input_or_output_iterator
in all cases.
Example
#include <iostream> #include <ranges> #include <vector> template <std::ranges::input_range R> void print(R&& r) { if (std::ranges::empty(r)) { std::cout << "\tEmpty\n"; return; } std::cout << "\tElements:"; for (const auto& element : r) { std::cout << ' ' << element; } std::cout << '\n'; } int main() { { auto v = std::vector<int>{1, 2, 3}; std::cout << "1. calling ranges::empty on std::vector:\n"; print(v); v.clear(); print(v); } { std::cout << "2. calling ranges::empty on std::initializer_list:\n"; auto il = {7, 8, 9}; print(il); print(std::initializer_list<int>{}); } { std::cout << "3. calling ranges::empty on a raw array:\n"; int array[] = {4, 5, 6}; // array has a known bound print(array); } }
Output:
1. calling ranges::empty on std::vector: Elements: 1 2 3 Empty 2. calling ranges::empty on std::initializer_list: Elements: 7 8 9 Empty 3. calling ranges::empty on a raw array: Elements: 4 5 6
See also
(C++17) |
checks whether the container is empty (function template) |