威海城乡和住房建设局网站,html5手机网站分辩率,聊城seo整站优化报价,免费企业建站开源系统目录 引言 库的基本功能
va_start 宏:
va_arg 宏
va_end 宏
va_copy 宏
使用 处理可变参数代码
C11可变参数模板
基本概念
sizeof... 运算符
包扩展 引言 在C编程中#xff0c;处理不确定数量的参数是一个常见的需求。为了支持这种需求#xff0c;C标准库提供了 11可变参数模板
基本概念
sizeof... 运算符
包扩展 引言 在C编程中处理不确定数量的参数是一个常见的需求。为了支持这种需求C标准库提供了 stdarg.h 头文件其中定义了一组宏和类型用于处理不定参数函数。C继承了C语言的可变参数机制使用了stdarg.h提供的宏来处理不确定数量的参数。其原理基于栈的推入和弹出过程不需要明确参数数量。此外C提供了可变参数机制让我们能够创建接收任意数量参数的函数。这一特性在许多实际应用中非常有用比如日志记录、函数重载等。 stdarg.h 库的基本功能
stdarg.h 库包含以下主要部分
va_start 宏:
用于初始化 va_list 变量其基本语法如下
void va_start(va_list ap, last);
apva_list 变量。last最后一个确定的参数后面的参数是可变参数。 va_arg 宏 va_arg 宏用于访问可变参数列表中的下一个参数其基本语法如下
type va_arg(va_list ap, type);
apva_list 变量。type要访问的参数的类型。
va_end 宏 va_end 宏用于结束 va_list 变量的访问其基本语法如下
void va_end(va_list ap); apva_list 变量。 va_copy 宏 va_copy 宏用于复制 va_list 变量其基本语法如下
void va_copy(va_list dest, va_list src);
dest目标 va_list 变量。src源 va_list 变量。
使用 stdarg.h 处理可变参数代码 示例中print_args接收一个格式字符串然后根据格式字符i表示整数d表示双精度浮点数解析后面的参数。
#include iostream
#include cstdargvoid print_args(const char* fmt, ...)
{va_list args;va_start(args, fmt);while (*fmt ! \0) {if (*fmt i) {int i va_arg(args, int);std::cout int: i std::endl;} elseif (*fmt d) {double d va_arg(args, double);std::cout double: d std::endl;}fmt;}va_end(args);
}int main()
{print_args(ddii, 0.618,3.14, 7, 9);return 0;
}
//double: 0.618 double: 3.14 int: 7 int: 9
C11可变参数模板
基本概念 C11通过模板提供了类型安全且灵活的可变参数机制。可以通过递归模板来处理不同类型的参数避免了手动处理类型的麻烦。也就是支持可变数量参数的函数模板和类模板可变数目的参数被称为参数包。
参数包有两种类型:
模板参数包表示零或多个模板参数使用class...或typename...关键字声明。函数参数包表示零个或多个函数参数使用类型名后跟...表示。
templateclass ...Arg void Func(Arg... arg) {}
templateclass ...Arg void Func(Arg... arg) {}
templateclass ...Arg void Func(Arg... arg) {} 我们用省略号...来指出一个模板参数或函数参数的表示一个包在模板参数列表中class...或typename...指出接下来的参数表示零或多个类型列表在函数参数列表中类型名后面跟...指出接下来表示零或多个形参对象列表。可变参数模板的原理跟模板类似本质还是去实例化对应类型和个数的多个函数。
sizeof... 运算符
sizeof...运算符来计算参数包中参数的个数。
template class ...Arg
void PrintArgNum(Arg... arg)
{cout sizeof...(arg)个参数包 endl;
}int main()
{double a 3.14;PrintArgNum();PrintArgNum(a);//一个参数PrintArgNum(1, string(241564132));//两个参数return 0;
} 编译本质会结合引用折叠规则实例化出以下三个函数在类型泛化基础上叠加了数量变化让泛型编程更加灵活。
void Print();
void Print(double arg1);
void Print(int arg1, string arg2);包扩展 对于一个参数包我们除了计算它的参数个数还可以对它进行包扩展。我们还要提供用于每个扩展元素的模式扩展一个包就是将他分解为构成的元素对每个元素应用模式获得扩展后的列表。我们通过在模式的右边放省略号(...)来触发扩展操作。
//参数包是0个时直接匹配这个函数
void ShowList()
{cout endl;
}templateclass T,class ...Arg
void ShowList(T val,Arg... arg)
{cout val endl;//arg是N个参数的参数包调用Printf,参数包的第一个传给val剩下N-1个传给参数包ShowList(arg...);
}
template class ...Arg
void Print(Arg... arg)
{cout sizeof...(arg)个参数包 endl;ShowList(arg...);
}int main()
{double x 3.14;Print();Print(11.1);//一个参数Print(1, string(bjkbhv));//两个参数Print(12.55, string(9jjug7), x);//三个参数return 0;
} 实际上是通过递归展开来实现的当参数包为空时就会调用 void ShowList()同时终止递归
递归时T接受传来参数包的第一个参数类型arg接受其余的参数类型以此往复。
文章转载自: http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.pfnrj.cn.gov.cn.pfnrj.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.pbknh.cn.gov.cn.pbknh.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.snrbl.cn.gov.cn.snrbl.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.brjq.cn.gov.cn.brjq.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn