一、可变参数模板
#include <iostream>
using namespace std;
template <typename... T>
void func1(T... args)
{
cout << sizeof...(T) << endl;
cout << sizeof...(args) << endl;
}
void func2()
{
}
template <typename T, typename... U>
void func2(T v, U... args)
{
cout << v << endl;
func2(args...);
}
int main()
{
func2(1, 2, 3);
system("pause");
return 0;
}
二、c库 "stdarg.h"
#include <stdarg.h>
#include <iostream>
using namespace std;
void func(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto a = va_arg(ap, int);
auto b = va_arg(ap, double);
auto c = va_arg(ap, char*);
cout << a << ", " << b << ", " << c << endl;
va_end(ap);
}
int main()
{
func("%d %f %s\n", 1, 2.0f, "hello world");
system("pause");
return 0;
}