给定一个 C++11 函数:
X f(A, B, C);
这个函数中有没有:
template<typename T>
void g(T t)
{
...
}
调用如下:
g(f);
确定:
- f的参数个数
- f的参数i的类型
- f的返回类型
...
template<typename F>
void g(F f)
{
constexpr size_t n = num_params<F>::n; // 3
return_type<F>::type x; // X
tuple<param_types<F>::type...> a; // tuple<A, B, C>
}
?
最佳答案
当然:
template <typename R, typename ...Args>
void g(R(&f)(Args...))
{
typedef R return_type;
unsigned int const n_args = sizeof...(Args);
// ...
}
用法:
int foo(char, bool);
g(foo);
关于c++ - 确定参数的数量和类型以及作为函数的类型参数的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998227/