Since ranges were merged into C++ 20, I've been looking through headers to see how operator|
is overloaded for range views, but I can't find the right track on how or where it is implemented.
C(R)
is equivalent to R | C
according to https://en.cppreference.com/w/cpp/ranges, based on what I've read.
Or, V(R, F)
is equivalent to R | V(F)
, where V
is the range adaptor object, R
is the viewable range, and F
is any callable object including free functions.
I've already read What are the basic rules and idioms for operator overloading? and already know how to overload operators properly, but I can't find the guide on what function arguments or template arguments are needed, or the syntactical meaning, to fulfill the requirement of the said implementation.
My point here is not to replace every bit of info from range adaptors, but only its syntactical feature on adaptor ranges with overloaded operator|
.
Another is that the resulting object type after the pipes must be the same as the type of container, as long as it fulfills the following concepts.
My mere idea:
template <std::ranges::viewable_range Ran, std::invocable Inv>
auto operator|(Ran cont, Inv func) {
/* ... */
}
Some Applications (supposed we already implemented the functions):
std::vector<int> vect1 {10, 3, 5, 3};
auto new_vect1 = vect1 | myfilter([](auto x){ return x % 3 == 0; });
// where new_vect1 is still `std::vector<int>`
where myfilter
:
template <std::viewable_range T, std::invocable F>
decltype(auto) myfilter(T container, F func);
But I think my idea is either entirely wrong, or wrong in some aspects, and the majority of this has been missing out.