The pipe operator

Let's see how we can implement a simple pipe operator so that we can write the following:

auto numbers = std::vector<int>{1, 3, 5, 7, 9}; 
auto seven = 7; 
bool has_seven = numbers | contains(seven); 

The contains function used with a pipeable syntax has two arguments: numbers and seven. As the left argument, numbers could be anything; we need the overload to contain something unique on the right side.

So, we create a struct named ContainsProxy, which holds onto the right argument; this way, the overloaded pipe operator can recognize the overload:

template <typename T>struct ContainsProxy { const T& value_; };template <typename Range, typename T>auto operator|(const Range& r, const ContainsProxy<T>& proxy) { const auto& v ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.