Advertisement

学习STL源码遇到了C++的新知识点(三)关于可变参数

阅读量:

写在前面

于array类的源代码中,发现存在如下代码片段

复制代码
 template<class _First,

    
 	class... _Rest>
    
 	array(_First, _Rest...)
    
 		-> array<typename _Enforce_same<_First, _Rest...>::type, 1 + sizeof...(_Rest)>;
    
    
    
    

因此,决定进一步研究C++中变长参数的使用方式。

变长参数

C++11引入了可变参数这一新特性,使得函数的输入参数数量可以不固定,通常采用“...”符号进行表示。

复制代码
    void fun(int start, ...)
    

如上述示例代码所示,这定义了一个具备可变参数功能的函数,其首个参数为start,并可接收数量不固定的int类型参数。

接下来通过一段代码进行说明

复制代码
 #include <iostream>

    
 #include<stdarg.h>
    
 using namespace std;
    
  
    
 void fun(int start, ...) {
    
 	va_list args;

全部评论 (0)

还没有任何评论哟~