如何从"decltype()"获取函数参数的数量<funtion>?

How to get number of function arguments from `decltype(<funtion>)`?

本文关键字:lt funtion gt 参数 decltype 函数 获取      更新时间:2023-10-16

假设我有以下函数声明:

template<typename signature>
int foo();

给定上述函数,是否可以以这种方式定义foo,从而返回在decltype模板参数中传递的函数参数的数量?

因此,示例用法可能看起来像:

int bar(int a, int b) 
{
return a + b;
}
int jar(int a)
{
return a * a;
}
int main() 
{
std::cout << foo<decltype(bar)>() << std::endl; // Desired output: 2
std::cout << foo<decltype(jar)>() << std::endl; // Desired output: 1
}

编辑:

谢谢大家的回复。它们似乎确实有效。然而,我忘了再提到一个用例。

假设我想得到以下函数的参数数量:

int __stdcall car(int a, int b, int c)
{
return a * b + c;
}

到目前为止,答案似乎不适用于这种使用__stdcall约定的函数。

你知道为什么以及可以做些什么吗?

为此(即使用decltype(,给定的foo是不够的。你需要以下特质。

template<typename> struct funtion_args final {};
template<typename ReType, typename... Args>
struct funtion_args<ReType(Args...)> final
{
static constexpr std::size_t noArgs = sizeof...(Args);
};

在变量模板参数上使用sizeof...运算符,以获得参数的数量。然后你可以像一样直接得到参数计数

std::cout << funtion_args<decltype(bar)>::noArgs << "n"; // output: 2

或装入foo

template<typename signature>
constexpr std::size_t foo() noexcept
{
return funtion_args<signature>::noArgs;
}

(请参阅实时演示(


更好的方法

如果你想要更少的类型(即没有decltype(,这是一种更方便的获得自由函数参数计数的方法,你可以执行以下

template <typename ReType, typename... Args> 
constexpr auto foo(ReType(*)(Args...)) noexcept
{
return sizeof...(Args);
}

现在,您可以方便地使用其他函数作为参数来调用foo

std::cout << foo(bar) << "n"; // output: 2

(请参阅实时演示(

当然,只需对合适的特征类型进行foo()调用即可。例如:

template <typename T>
struct foo_helper;
template <typename T, typename... Args>
struct foo_helper<T(Args...)> {
static constexpr std::size_t arg_count = sizeof...(Args);
};
template <typename T>
std::size_t foo() {
return foo_helper<T>::arg_count;
}