Home > Software engineering >  void(os << args). What does void mean in this context?
void(os << args). What does void mean in this context?

Time:11-17

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.

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related