You can get that information by using a variadic function template.
#include <iostream>
template <typename R, typename ... Types> constexpr size_t getArgumentCount( R(*f)(Types ...))
{
return sizeof...(Types);
}
//----------------------------------
// Test it out with a few functions.
//----------------------------------
void foo(int a, int b, int c)
{
}
int bar()
{
return 0;
}
int baz(double)
{
return 0;
}
int main()
{
std::cout << getArgumentCount(foo) << std::endl;
std::cout << getArgumentCount(bar) << std::endl;
std::cout << getArgumentCount(baz) << std::endl;
return 0;
}
Output:
3
0
1
See it working at http://ideone.com/oqF8E8.
Update
Barry suggested use of:
template <typename R, typename ... Types>
constexpr std::integral_constant<unsigned, sizeof ...(Types)> getArgumentCount( R(*f)(Types ...))
{
return std::integral_constant<unsigned, sizeof ...(Types)>{};
}
With this, you can get the number of argument by using:
// Guaranteed to be evaluated at compile time
size_t count = decltype(getArgumentCount(foo))::value;
or
// Most likely evaluated at compile time
size_t count = getArgumentCount(foo).value;