C++模板打印輸出,任意個數,任意類型

#include <iostream>

void printX()
{

}

template <typename T, typename ... Args>
void printX(T t, Args...args)
{
	std::cout << t << "\t剩餘參數個數:" << sizeof...(args) << std::endl;

	printX(args...);
}

int main()
{
	printX("hello ", 134, 'a', 5.6);

	return 0;
}

輸出

hello   剩餘參數個數:3
134     剩餘參數個數:2
a       剩餘參數個數:1
5.6     剩餘參數個數:0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章