use*_*989 3 c++ boost c++14 boost-hana
我知道如何以旧的方式获取函数的参数类型,但我想知道是否有一个很好的新方法与Hana一起做?例如,我想要这样的东西:
struct foo {
int func(float);
};
auto getFuncType(auto t) -> declval<decltype(t)::type>()::func(TYPE?) {}
getFunType(type_c<foo>); // should equal type_c<float> or similar
Run Code Online (Sandbox Code Playgroud)
我如何到达TYPE这里?
编辑2016年6月21日 - 轻微更改以匹配库的当前版本(0.4).
我是CallableTraits的作者,这是@ildjarn上面提到的库(尽管它尚未包含在Boost中).的arg_at_t元函数的是,我知道获得从一个成员函数,函数,函数指针,函数引用,或函数对象/λ参数类型的最佳方式.
请记住,该库目前正在进行重大更改,并且链接的文档有些过时(即使用时风险自负).如果您使用它,我建议克隆开发分支.对于您正在寻找的功能,API几乎肯定不会改变.
对于成员函数指针,arg_at_t<0, mem_fn_ptr>别名相当于decltype(*this),以说明隐式this指针.所以,对于你的情况,你会这样做:
#include <type_traits>
#include <callable_traits/arg_at.hpp>
struct foo {
int func(float);
};
using func_param = callable_traits::arg_at_t<1, decltype(&foo::func)>;
static_assert(std::is_same<func_param, float>::value, "");
int main(){}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将其放入一个boost::hana::type或任何用例所需的内容中.