ramgorur
ramgorur

Reputation: 2162

Explanation of a variadic parameter while overloading << operator for std::tuple

I was going through an example of how to overload operator<< for std::tuple in this cpp reference link.

Here is the example code:

template<typename... Ts>
std::ostream& operator<<(std::ostream& os, std::tuple<Ts...> const& theTuple)
{
    std::apply
    (
        [&os](Ts const&... tupleArgs)
        {
            os << '[';
            std::size_t n{0};
            ((os << tupleArgs << (++n != sizeof...(Ts) ? ", " : "")), ...);
            os << ']';
        }, theTuple
    );
    return os;
}

I don't quite understand the line ((os << tupleArgs << (++n != sizeof...(Ts) ? ", " : "")), ...);.

I understand that (os << tupleArgs << (++n != sizeof...(Ts) ? ", " : "")) is printing the parameters separated by comma except the last parameter.

But I have no idea what is the ellipsis (...) is doing at the end (variadic parameter?, but why?).

Also why is the statement enclosed in a bracket like this (print statement, ellipsis);?

Can anyone please explain?

Upvotes: 0

Views: 31

Answers (0)

Related Questions