c++ 函数模板的两种用法,第二种是可变参数个数的使用方法,其中sizeof...() 函数可以获取输入可变参数的数量
#include <iostream>
template<typename T>
T AddMyNum(const T &a, const T& b)
{
return a+b;
}
template<typename T,typename... Args>
T AddMyNum(const T &a, const T& b,Args... arg)
{
if(sizeof...(arg)==0)
{
return a+b;
}
std::cout<<"current input nums is:"<<sizeof...(arg)<<std::endl;
AddMyNum(arg...);
}
int main()
{
auto temp = AddMyNum(1,2);
std::cout<<"add 1+2:"<<temp<<std::endl;
std::cout<<"quit"<<std::endl;
auto temp1 = AddMyNum(1,2,3,4,5,6);
std::cout<<"add 1+2 3,4,5,6:"<<temp1<<std::endl;
return 0;
}
标签:...,const,c++,template,简单,sizeof,模板 From: https://www.cnblogs.com/52ld/p/17184372.html