C++模板取函数参数类型和返回值类型的方法

假设要利用模板元编程获取位于index的参数的类型:

template<int index, class FuntionType>
struct ArgTypeAt
{
    // FuntionType的返回值类型和参数类型?
};

int f(int, short, float);
static_assert(is_same_v<decltype(f), int(int, short, float)>);
using Arg2Type = ArgTypeAt<2, decltype(f)>::type; // 假设这样使用模板

这时FunctionType就是一个单独的类型int(int, short, float)了,里面含有各参数的类型。要把FuntionType分离成返回值类型和参数类型,方法是利用模板特化,然后参数类型是一个包,再把参数包展开就能得到各位置参数的类型:

template<int index, class FuntionType>
struct ArgTypeAt;

template<class ResultType, class FirstArgType, class... ArgsType>
struct ArgTypeAt<0, ResultType(FirstArgType, ArgsType...)>
{
    using type = FirstArgType;
};

template<int index, class ResultType, class FirstArgType, class... ArgsType>
struct ArgTypeAt<index, ResultType(FirstArgType, ArgsType...)>
{
    using type = typename ArgTypeAt<index - 1, ResultType(ArgsType...)>::type;
};

int f(int, short, float);
static_assert(is_same_v<ArgTypeAt<2, decltype(f)>::type, float>); // OK

匹配过程是:ArgTypeAt<2, int(int, short, float)>::type = ArgTypeAt<1, int(short, float)>::type = ArgTypeAt<0, int(float)>::type = float

还有个问题,如果把f的调用约定(默认是__cdecl)改成__stdcall这个模板特化就不匹配了,因为修饰符也是类型的一部分,而C++的泛型并没有修饰符变了还能匹配的方法(只有类型变了能匹配)。参考标准库的std::function定义了一堆宏,我也用宏改造成下面这样:

template<int index, class FuntionType>
struct ArgTypeAt;

#define DEF_ARG_TYPE_AT(CV) \
    template<class ResultType, class FirstArgType, class... ArgsType>                   \
    struct ArgTypeAt<0, ResultType CV(FirstArgType, ArgsType...)>                       \
    {                                                                                   \
        using type = FirstArgType;                                                      \
    };                                                                                  \
                                                                                        \
    template<int index, class ResultType, class FirstArgType, class... ArgsType>        \
    struct ArgTypeAt<index, ResultType CV(FirstArgType, ArgsType...)>                   \
    {                                                                                   \
        using type = typename ArgTypeAt<index - 1, ResultType CV(ArgsType...)>::type;   \
    };

DEF_ARG_TYPE_AT(__cdecl)
#ifdef _M_CEE
DEF_ARG_TYPE_AT(__clrcall)
#endif
#if defined(_M_IX86) && !defined(_M_CEE)
DEF_ARG_TYPE_AT(__fastcall)
#endif
#ifdef _M_IX86
DEF_ARG_TYPE_AT(__stdcall)
#endif
#if ((defined(_M_IX86) && _M_IX86_FP >= 2) \
    || defined(_M_X64)) && !defined(_M_CEE)
DEF_ARG_TYPE_AT(__vectorcall)
#endif

// OK
static_assert(is_same_v<ArgTypeAt<2, int(int, short, float)>::type, float>);
static_assert(is_same_v<ArgTypeAt<2, int __cdecl(int, short, float)>::type, float>);
static_assert(is_same_v<ArgTypeAt<2, int __fastcall(int, short, float)>::type, float>);
static_assert(is_same_v<ArgTypeAt<2, int __stdcall(int, short, float)>::type, float>);
static_assert(is_same_v<ArgTypeAt<2, int __vectorcall(int, short, float)>::type, float>);

还有其他修饰符constvolatilenoexcept、引用、成员函数同理,这里就不写了

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值