C++ Ranges::empty_view , enable_view, filter_view Introduction














































C++ Ranges::empty_view , enable_view, filter_view Introduction



In this series of articles on RANGES with C++, today we are going to see libraries of std::ranges::empty_view , std::ranges::enable_view and std::ranges::filter_view
____________________________________________________________________________________

INTRODUCTION to RANGES LIBRARY HAS BEEN DONE IN MY PREVIOUS ARTICLES.

All the libraries defined below are included in the header <ranger> and <iterator>.


std::ranges::empty_view


First lets understand what is Argument Defined Lookup(ADL)::

It is the set of rules for looking up the unqualified function names in function-call expressions,including function call.

Argument-dependent lookup makes it possible to use operators defined in a different namespace. For Example:

If we use ::

std::cout << "Hello\n"; // There is no operator<< in global namespace, but ADL examines std namespace because     the left argument is in \std and finds std::operator<<(std::ostream&, const char*)
operator<<(std::cout, "Hello\n"); // same, using function call notation.
Empty_view are functions that are not visible to ordinary lookup, and can only be found by argument-dependent lookup when std::ranges::empty_view<T> is an associated class of the arguments.
Example Code
#include <ranges>
int main()
{
    std::ranges::empty_view<int> a;
    static_assert(std::ranges::empty(a));
    static_assert(0 == a.size());
}
 std::ranges::enable_view
The enable_view variable template is used to indicate that whether a Range is a View.
(definition of Views has been done in my first article of ranges libraries.)
A type is considered a view if it is unambiguously derived from view_base by default.
For a  For a type T, the default value of enable_view<T> is:
If Deriiif DerivedFrom<T, view_base> is true, enable_view<T> is true.
std::ranges::filter_view
  •  base_ and pred_ are default initialisers.
  •  base_  :=  std::move(base) and     pred_  =   std::move(pred).
  •  Initializes base_ with view::all(std::forward<R>(r)) and initializes pred_ with std::%u200Bmove(pred).

Its Parameters are:

  • pred - predicate to filter elements.
  • r - range to filter.






Comments