1、变长参数模板。
#include <iostream>
using namespace std;
template <class T>
void myprint(T v)
{
cout<<v;
}
template <typename T,typename... TAIL>
void myprint(T t,TAIL... tail)
{
cout<<t;
myprint(tail...);
}
int main(int argc,char* argv[])
{
myprint("hello",222,"world",3.0f);
return 0;
}
获取可变参数个数:
template <typename... Args>
unsigned int get_param_count(Args... args)
{
return sizeof...(args);
}
2、初始化列表initializer_list
#include <iostream>
#include <initializer_list>
using namespace std;
void myprint2(initializer_list<int> l)
{
for (auto i=l.begin();i!=l.end() ;++i )
{
cout<<*i<<" ";
}
}
int main(int argc,char* argv[])
{
myprint2({1,2,3,4});
return 0;
}