Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void(os << args). What does void mean in this context?

Tags:

c++

I was reading about folding expressions and found an example of what was used before folding expressions:

template <class... Ts>
void print_all(std::ostream& os, Ts const&... args) {
    using expander = int[];
    (void)expander{0,
        (void(os << args), 0)...
    };
}

The problem is the void(os << args) bit. What does void mean in this context? I've already tried to search for this, but it is pretty generic.

Thanks for your time.

like image 416
Octo Avatar asked Nov 16 '21 12:11

Octo


People also ask

What does void * mean in C?

void* is a "pointer to anything". void ** is another level of indirection - "pointer to pointer to anything". Basically, you pass that in when you want to allow the function to return a pointer of any type.

What does void mean in C++?

When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters.


1 Answers

It casts the result of (os << args) to void. That's it.

This style is used to prevent the very rare overload of ,(comma) operator since in theory, << can be overloaded for user-defined args argument and return something that has overloaded the comma operator for X, 0 expression. That might break the initialization trick. This way, the code is safe and comma is always used as the ordinary sequencing operator.

like image 130
Quimby Avatar answered Oct 16 '22 22:10

Quimby