能夠接受任意多個參數的函數,可以利用重載來實現。這種函數的執行過程類似於遞歸調用,所以必須要有遞歸終止條件。本文特意爲大家收集整理了C++中可以接受任意多個參數的函數定義方法,希望大家喜歡!
#include <iostream>
#include <bitset>
void print() {} // 遞歸終止條件。這是必需的.。
template<typename Type, typename... Types>
void print(const Type& arg, const Types&... args)
{
std::cout << arg << std::endl;
print(args...);
}
int main()
{
print(1, 3.1415, "Hello, world!", 1.618, true, std::bitset<16>(377), 40);
return 0;
}
執行後的結果如下:
1
3.1415
Hello, world!
1.618
1
0000000101111001
40