Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 means in this context? I’ve already tried to search for this, but it is pretty generic.

Thanks for your time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading