What Does the Vertical Pipe ( | ) Mean in C++

What does the vertical pipe ( | ) mean in C++?

Bitwise OR operator. It will set all bits true that are true in either of both values provided.

For example CS_HREDRAW could be 1 and CS_VREDRAW could be 2. Then it's very simple to check if they are set by using the bitwise AND operator &:

#define CS_HREDRAW 1
#define CS_VREDRAW 2
#define CS_ANOTHERSTYLE 4

unsigned int style = CS_HREDRAW | CS_VREDRAW;
if(style & CS_HREDRAW){
/* CS_HREDRAW set */
}

if(style & CS_VREDRAW){
/* CS_VREDRAW set */
}

if(style & CS_ANOTHERSTYLE){
/* CS_ANOTHERSTYLE set */
}

See also:

  • Wikipedia: Bitwise operation (Section OR)
  • Wikipedia: Mask (computing) Section( Common bitmask functions)

What does the vertical pipe | mean in the context of c++20 and ranges

This sort of function chaining has been introduced with C++20 ranges, with the biggest feature allowing lazy evaluation of operation on views (more precisely, viewable ranges). This means the operation transforming the view will only act on it as it is iterated.

This semantic allows for the pipeline syntax sugar, putting in a readable way what will happen when the result is iterated. The functions this is used with are based on range adaptors, which take a view (and possibly additional arguments after it) and transform it as they are iterated (essentially returning another view).

The pipeline syntax is reserved for a special sub group of these called range adaptor closures, which only take a single view with no additional parameters. These can be either adaptors with no additional arguments, adaptors with the excess arguments bound, or the result of some library functions such as the std::views::transform in the OP. Since cpp23 you can also define these yourself). Once we have some of these, the syntax:

some_viewable_range | std::views::some_adaptor_closure | some_other_adaptor_closure

is equivalent to

some_other_adaptor_closure(std::views::some_adaptor_closure(some_viewable_range))

which will evaluate the pipeline as the returned view is iterated. Similarly,

some_vector | std::views::transform([](T x){ return x*x; });

is the same as

std::views::transform([](T x){ return x*x; })(some_vector); // The first call returns the adaptor std::views::transform(some_vector, [](T x){ return x*x; }) with the second argument bound.

but more readable.

Like any view you can iterate them directly. Since this is lazy bad things can happen such as:

template<typename T>
auto square_vector(const std::vector<T> &some_vector) {
return some_vector | std::views::transform([](T x){ return x*x; });
}

int main () {
for(auto val : square_vector(std::vector<int>{1, 2 ,3, 4, 5}))
std::cout << val << '\n';
}

by the time you get to print your val, the original vector does not exist, so the input to the chain is gone, and it goes down hill from there.

To delve further into the world of ranges and adaptors you can check https://en.cppreference.com/w/cpp/ranges, and the original library these were based on, https://ericniebler.github.io/range-v3/.

What does | (pipe) mean in c#?

It is normally a bitwise or operator. In this context, it's used on an enum with the flags attribute set.

What is the exact function of the | (vertical line) operator when separating arguments

| is the bitwise or operator. It's used in the way you describe when multiple values can be combined to produce different effects. For example:

unsigned char MB_ICONWARNING = 1; //00000001
unsigned char MB_CANCELTRYCONTINUE = (1 << 1); //00000010
unsigned char MB_DEFBUTTON2 = (1 << 2); //00000100

Let's say you want a message box with that has all the properties represented by those values, you can specify that by bitwise or'ing them:

unsigned char combined = MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2; //00000111

The called function can then use them to determine the options you requested with the bitwise & operator like this

if(options & MB_ICONWARNING)
{
//Do MB_ICONWARNING
}

if(options & MB_CANCELTRYCONTINUE)
{
//Do MB_CANCELTRYCONTINUE
}

//etc...

If you're interested, you can read more on Bit Fields.

C++ - What is the pipe ( | ) character?

| is a bitwise OR.

Libraries generally define different masks, such as your wxEXPAND and wxALL which are usually integer constants with only one bit set.

When you use the bitwise-or operator, you can combine these to create what's called a bitfield, an integer with bits that you define set.

You combine these like this:

wxEXPAND | wxALL

which will create a bitfield with the bits from wxEXPAND and wxALL set.

Usually the library will then check if these bits are set like this for example:

if (bitfield & wxEXPAND) { .. // wxEXPAND is set

This is a bitwise AND. The test will return true if and only if the wxEXPAND bit is set in bitfield.

Libraries use this to allow you to pass multiple options in a single register, for example.

What does a vertical bar | (aka pipe) accomplish in OCaml?

In your example it serves as a pattern separator for a multiple selection statement, sorta like the case of a switch statement in C like languages.

let is_vowel c = match c with 
'a' | 'e' | 'i' | 'o' | 'u' -> true
| _ -> false ;;

function is_vowel(c){
switch(c){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
default:
return false;
}
}

These 2 pieces of code would generate the same output, however this is where the similarity's end. I found this documentation to be very helpful, it go's more into detail what else you can or cannot do with match.

Other uses of the pipe operator are:

  • enumerated type declaration

    • type typ = N1 | N2 | N3
  • union type declaration

    • type typ = N1 of typ1 | N2 of typ2

What is mean pipe operator between 2 int variants?

It did a bitwise or of the two values.



Related Topics



Leave a reply



Submit