实现一
#include <iostream>
#include <list>
#include <string>
using namespace std;
// 定义一个空的print函数,作为递归终止条件
void print(){
//
};
// 可变参数模板函数,用于递归输出传入的参数
template <typename T, typename... OtherTypes>
void print(const T& _arg, const OtherTypes&... _args) {
// 输出第一个参数_arg
cout << _arg;
// 递归调用print函数,将剩余的参数_args传入
print(_args...);
}
实现二
template <typename T>
void printTwo(T arg) {
cout << arg;
}
template <typename T, typename... OtherTypes>
void printTwo(int count, const T& arg, const OtherTypes&... args) {
cout << arg;
if (count > 1) {
cout << ' ';
}
printTwo(count - 1, args...);
}