English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A function that can accept any number of parameters can be implemented using overloading. The execution process of this function is similar to recursive calls, so there must be a recursive termination condition.
#include <iostream> #include <bitset> void print() {} // Recursive termination condition. This is necessary. 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; }
The result after execution is as follows:
1 3.1415 Hello, world! 1.618 1 0000000101111001 40
That's all the C++This is all about the method of defining a function that can accept any number of parameters (detailed explanation), I hope everyone will support and cheer for the tutorial!