C++可變參數列表

#include <iostream>

using namespace std;

//停止解析參數列表
void function()
{

}

template <typename T, typename... U>
void function(T t, U... args)
{
	//解析包的大小
	cout << "the numbers of args is " << sizeof...(U) + 1  << ends;
	//cout << "the size of args is " << sizeof...(args) + 1 << ends;

	cout << t << endl;
	function(args...);
}
int main()
{
	function("123", "456", "789", "10 11", "12 13");
	system("pause");
}
//結果
//the numbers of args is 5 123
//the numbers of args is 4 456
//the numbers of args is 3 789
//the numbers of args is 2 10 11
//the numbers of args is 1 12 13
//請按任意鍵繼續. . .

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章