网站首页该怎么做,网站内容与模板设计,wordpress速度慢谷歌字体,深圳设计培训前言 可变参数模板是C11中的新特性#xff0c;它能够让我们创建可以接收可变参数的函数模板和类模板#xff0c;相比C98/03#xff0c;类模版和函数模版中只能含固定数量的模版参数#xff0c;可变模版参数是一个巨大的改进#xff0c;通过系统系统推演数据的类型#xf…前言 可变参数模板是C11中的新特性它能够让我们创建可以接收可变参数的函数模板和类模板相比C98/03类模版和函数模版中只能含固定数量的模版参数可变模版参数是一个巨大的改进通过系统系统推演数据的类型不用自己写出模板参数类型。 下面我们来创建一个基本的的能够接收可变参数的函数模板
templateclass ...Args
void ShowFunc(Args... args)
{
}
声明一个参数包Args... args这个参数包中可以包含0到任意个模板参数。
上面代码中Args是一个模板参数包args是一个函数形参参数包。
#includeiostream
#includestring
using namespace std;
templateclass ...Args
void ShowFunc(Args... args)
{cout sizeof...(args) endl;
}
int main()
{ShowFunc(1);ShowFunc(1, 1.1);ShowFunc(1, 1.1, A, std::string(sort));return 0;
}
sizeof...(args)是计算该函数中含有多少个参数即计算函数形参的个数。 下面我们来看一下如何使用可变参数的函数模板获取其参数包的值。 方法一编译器编译时的递归推演第一个模板参数依次解析获取参数包中的参数值。 #includeiostream
#includestring
using namespace std;// 递归终止函数
template class T
void ShowFunc(const T t)
{cout t endl;
}
// 展开函数
template class T, class ...Args
void ShowFunc(T value, Args... args)
{cout value ;ShowFunc(args...);
}
int main()
{ShowFunc(1);ShowFunc(1, 1.1);ShowFunc(1, 1.1, A, std::string(sort));return 0;
} 方法二利用逗号表达式展开参数包初始化数组arr强行让编译器解析参数包参数包有几个参数PrintArg就自动推演生成相应个。 #includeiostream
#includestring
using namespace std;template class T
void PrintArg(T t)
{cout t ;
}
//展开函数
template class ...Args
void ShowFunc(Args... args)
{int arr[] { (PrintArg(args), 0)... };cout endl;
}
int main()
{ShowFunc(1);ShowFunc(1, A);ShowFunc(1, A, std::string(CSDN));return 0;
} 文章转载自: http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.vjwkb.cn.gov.cn.vjwkb.cn http://www.morning.rlnm.cn.gov.cn.rlnm.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.zdbfl.cn.gov.cn.zdbfl.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.pnmgr.cn.gov.cn.pnmgr.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.nmtyx.cn.gov.cn.nmtyx.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.sooong.com.gov.cn.sooong.com http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com