Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to print a variadic parameter pack using std::ostream?

What is the easiest way to print a parameter pack, separated by commas, using std::ostream?

Example:

template<typename... Args>
void doPrint(std::ostream& out, Args... args){
   out << args...; // WRONG! What to write here?
}

// Usage:
int main(){
   doPrint(std::cout,34,"bla",15); // Should print: 34,bla,15
}

Note: It may be assumed that a corresponding overload of the << operator is available for all types of the parameter pack.

like image 797
gexicide Avatar asked Dec 09 '14 09:12

gexicide


2 Answers

Without recursive calls and commas where you wanted.

In c++11 / c++14 through parameter pack expansion:

template <typename Arg, typename... Args>
void doPrint(std::ostream& out, Arg&& arg, Args&&... args)
{
    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander{0, (void(out << ',' << std::forward<Args>(args)), 0)...};
}

DEMO


In c++17 using fold expressions:

template <typename Arg, typename... Args>
void doPrint(std::ostream& out, Arg&& arg, Args&&... args)
{
    out << std::forward<Arg>(arg);
    ((out << ',' << std::forward<Args>(args)), ...);
}

DEMO 2

like image 166
Piotr Skotnicki Avatar answered Oct 23 '22 20:10

Piotr Skotnicki


In C++17, there will be an easier way (as hinted at by Kerrek SB in comments; this was actually present in N4606, the first post-C++14 draft), called fold expressions:

The code would be:

(out << ... << args);

and the pattern expression op ... op parameter-pack is called a binary left fold, whose definition is equivalent to ((( expression op arg1) op arg2) op arg3) .... op argN.

I think the outer parentheses are not strictly necessary for an expression-statement like this, but if the fold expression is an operand of another operator then they are either required, or a very good idea :)

like image 35
M.M Avatar answered Oct 23 '22 20:10

M.M