Problem
给定任何函数(或可调用)类型Function
,如何将其所有参数类型作为元组类型获取?
例如,我需要一个特质function_traits<Function>::arguments
, where:
int f();
typename function_traits<decltype(f)>::arguments // => gives me std::tuple<>
void g(int);
typename function_traits<decltype(g)>::arguments // => gives me std::tuple<int>
void h(int, int);
typename function_traits<decltype(h)>::arguments // => gives me std::tuple<int, int>
我的想法
First
我需要获得参数的大小,幸运的是 boost 已经实现了function_traits<F>::arity
Then
生成一个std::integer_sequence
从1到artify,映射到arguments类型,但是问题来了,要映射integer_sequence
,我需要这样的东西:
function_traits<F>::arg_type<N> // -> N-th arg_type
但 boost 只提供了这个:
function_traits<F>::argN_type
Question
我该如何实施function_traits<F>::arg_type<N>
?我可以使用 C++ 标准直至 c++17
像这样的东西:
#include <tuple>
template<typename x_Function> class
function_traits;
// specialization for functions
template<typename x_Result, typename... x_Args> class
function_traits<x_Result (x_Args...)>
{
public: using arguments = ::std::tuple<x_Args...>;
};
用法示例:
#include <type_traits>
int foo(int);
using foo_arguments = function_traits<decltype(foo)>::arguments;
static_assert(1 == ::std::tuple_size<foo_arguments>::value);
static_assert(::std::is_same_v<int, ::std::tuple_element<0, foo_arguments>::type>);
在线编译器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)