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.
Without recursive calls and commas where you wanted.
In c++11 / c++14 through parameter pack expansion:
DEMO
In c++17 using fold expressions:
DEMO 2