P
P
Pavel K2016-12-22 13:22:26
C++ / C#
Pavel K, 2016-12-22 13:22:26

Variable number of function parameters (variadic template), how to make recursion?

Greetings!
I want to make a function with a variable number of arguments, which would be called like this:
functionName(param1, param2, paramN);
All parameters are of the same type, but their total number is unknown.
I am writing a template:

template<typename... Args>
void functionName(double arg1,  Args ... args) {
   ....
   return functionName(args...); 
}
 void functionName() {}

According to my logic, the number of parameters in args... should decrease each time, because the first parameter is explicitly declared, but this does not happen and the function call loops forever with the same number of parameters when called (at the first call, 3 was passed, in the future it remains 3). Tell me how to implement correctly?
UPDT: The error was mine, everything works: cpp.sh/6mamm (PS volatile will be superfluous)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-12-22
@xpert13

Instead of a variable number of arguments, you can use an array or some kind of container, in which to write all the values ​​of these arguments.

M
maaGames, 2016-12-22
@maaGames

Judging by the code, only doubles can be arguments, so templates are not needed at all, the classic function with a variable number of arguments will suffice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question