c++ - 确定参数的数量和类型以及作为函数的类型参数的返回类型

标签 c++ c++11

给定一个 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/

相关文章:

c++ - C++不能为子类编写构造函数

c++ - 使用 ICU (ICU4C) 读取 UTF-8 编码文件的缓冲区大小

c++ - MySQL 不正确的日期时间值 : '12/31/2016 23:59:59' for function str_to_date

c++ - 为什么 C++ Builder 不能编译它?

c++ - 为什么 `std::array::at()` 没有实现为模板函数?

c++ - Visual Studio for C++ __cplusplus 支持的语言显示为 C++98

c++ - 使用 constexpr 或 struct 进行元编程

C++ libclang : Retrieving cursor from CXSourceLocation returning wrong cursor?

c++ - 列出 boost io_service 中的事件处理程序

c++ - 如何在多个类中使用相同的 unique_ptr