前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++模板取函数参数类型和返回值类型的方法

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

作者头像
用户7886150
修改2021-02-03 10:39:40
4.1K0
修改2021-02-03 10:39:40
举报
文章被收录于专栏:bit哲学院

参考链接: 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>); 

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

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档