可变参数函数模板

16.53 编写你自己版本的print函数,并打印一个、两个及五个实参来测试它,要打印的每个实参都应有不同的类型。

#include<iostream>
#include<string>
using namespace std;

template <typename T>
ostream& print(ostream &os,const T &t)
{
    os<<t;
    return os;
}

template <typename T,typename ... Args>
ostream& print(ostream &os,const T &t,const Args&...rest)
{
    os<<t<<" , ";
    return print(os,rest...);
}

int main()
{
    print(cout,1,3.14,"hello");
}
原文地址:https://www.cnblogs.com/wuchanming/p/3950482.html