能夠接受任意多個引數的函式,可以利用過載來實現。這種函式的執行過程類似於遞迴呼叫,所以必須要有遞迴終止條件。本文特意為大家收集整理了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