+ 1

Which way to detect amount of function arguments?

I'd like to use a function that I dont know how many argument will be pass to it. In C++, we have stdarg.h library, define macros to do this task. And we have to need something to detect amount of arguments or to stop the pointer va_list when it traverse to last argument. Example, Sum(amount, 2,4,4) or Sum(2,4,5,0). But I want to try a other approaches. Below is my code use try catch(), but no any exception threw. https://code.sololearn.com/cUX0cVyn166e/?ref=app

5th Apr 2018, 10:57 AM
Nguyễn BĂĄ TĂčng
Nguyễn BĂĄ TĂčng - avatar
1 Answer
+ 1
Don't do it. Pass a list instead. From documentation on the args issue: "Notice also that va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list). The function should be designed in such a way that the number of parameters can be inferred in some way by the values of either the named parameters or the additional arguments already read." http://www.cplusplus.com/reference/cstdarg/va_arg/
5th Apr 2018, 12:12 PM
Emma