Answer the question
In order to leave comments, you need to log in
How to find out the number of arguments passed in C++?
After python, js, for interest I look
at C ++ How can I find out the number of arguments in C ++?
For example, in python3 you can do this
f = lambda *args: print(len(args))
f(1,2,3) # выведет 3
Answer the question
In order to leave comments, you need to log in
Quick and easy. We count using variable patterns and the operatorsizeof
template <typename ... Args> int ArgsCount(Args ... args) {
return sizeof...(args);
}
std::cout << ArgsCount(1, "hello", 2.f); // Вывод: 3
C++ functions mostly have a fixed number of parameters, so there is no problem with determining the number of parameters - this is known at compile time.
Functions with a variable number of parameters (for example, printf(char* format, ...) ) have their own mechanism for determining the number of parameters, which you yourself must code at your discretion. You can submit the total number of arguments as the first argument, as you suggest.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question