0

This video suggests that the | is used to pass data through the functions

Is this something, the bitwise or can do, or did I miss something?

auto selection = data | drop(from)
                      | take(to - from)
                      | filter(predicate);

(See the second example: https://youtube.com/shorts/weGxJilk-c4)

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • 1
    This is a specific overload of the binary `|` operator for the C++20 [`ranges`](https://en.cppreference.com/w/cpp/ranges) library. – 0x5453 Oct 11 '21 at 21:34
  • 5
    bitwise or `|` is a normal operator that you can overload to your likings like most other operators, so yes you can use it as a pipe. C++20 already does this, you can google _C++20 ranges_. – Timo Oct 11 '21 at 21:34
  • Well, operators can be overloaded, so yes. – πάντα ῥεῖ Oct 11 '21 at 21:34
  • `|` names an operator, `operator|`. It can be used for bitwise OR for numeric/binary and similar types, or it can be freely overloaded to represent any functionality that's compatible with the structure of a binary operator. – nanofarad Oct 11 '21 at 21:34
  • Note that the order of evalations of the `|` operator is implementation-defined, so you may find that your `drop()`, `take()`, and `filter()` functions are not being called in the order you expected them to be called in, on some platforms! – Jeremy Friesner Oct 11 '21 at 21:36
  • Here's the thing, it's not bitwise or. It's the same token, but the semantics are completely different. Kinda like `>>` is right shift in core C++, but is "extraction" in the stream library. In this case, calling it a "pipe" would be more appropriate indeed. – StoryTeller - Unslander Monica Oct 11 '21 at 21:40
  • 1
    @JeremyFriesner • when did the associativity of [`operator|`](https://en.cppreference.com/w/cpp/language/operator_precedence) change from being left-to-right to being implementation-defined? – Eljay Oct 11 '21 at 22:50
  • 1
    @Eljay not the associativity; the order-of-operations. See: https://stackoverflow.com/questions/53513909/does-an-overloaded-bitwise-or-operator-have-a-well-defined-evaluation-orde – Jeremy Friesner Oct 11 '21 at 23:14
  • @JeremyFriesner • that makes sense, thank you. Piping shouldn't rely on sequencing, but I see how someone could have (inadvertently) put in some sequencing dependent code if they weren't careful. Sometimes C++ is a sharp knife. – Eljay Oct 12 '21 at 02:42

1 Answers1

3

Is this something, the bitwise or can do?

Yes. The bitwise or | acts like a bitwise or when used on integer types.

On other types, such as classes, this operator can be overloaded to have whatever behavior the class author wishes. In your code, it is operating on classes from the ranges library in the standard library, and you are right that it works similarly to a "pipe".

You have seen something very similar done with the bitshift operator << when used on classes derived from std::basic_ostream such as

std::cout << "Hello world!\n";
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180