当前位置: 首页 > news >正文

c++ 拆分函数返回值和参数类型

在c++中,函数参数类型和返回值类型通常是一个比较明确的信息,好像确实无需在这个上面费周折。然而,硬编码数据类型会让代码复用性下降,如果能够通过某种方式自动获取函数参数和返回值类型,对于代码的可复用性,可读性和整洁性都有较大的提升。最近阅读GoogleTest的源码(v1.8.1)发现,它采用了模板方法实现了这一点:

// googletest/googlemock/include/gmock/internal
/gmock-generated-internal-utils.h...
template <typename F>
struct Function;template <typename R>
struct Function<R()> {typedef R Result;...
};template <typename R, typename A1>
struct Function<R(A1)>: Function<R()> {typedef A1 Argument1;...
};template <typename R, typename A1, typename A2>
struct Function<R(A1, A2)>: Function<R(A1)> {typedef A2 Argument2;...
};template <typename R, typename A1, typename A2, typename A3>
struct Function<R(A1, A2, A3)>: Function<R(A1, A2)> {typedef A3 Argument3;...
};...

上面的代码,gmock在使用的时候是配合宏实现不同类型函数的统一。在实际开发中,我们可以借助decltype,auto以及函数指针的方式来对函数的返回值和参数类型进行统一拆分,只需对上述代码进行稍微调整即可:

template <typename T> 
struct Function;//以下以__stdcall 调用类型为例,如果函数调用类型是 __cdcel, 则需要特化新的模板组
template <typename R>
struct Function<R __stdcall()> {typedef R RESULT;
};template <typename R, typename A1>
struct Function<R __stdcall(A1)> :Function<R __stdcall()>
{typedef A1 ARG1;
};template <typename R, typename A1, typename A2>
struct Function<R __stdcall(A1,A2)> :Function<R __stdcall(A1)>
{typedef A2 ARG2;
};template <typename R, typename A1, typename A2,typename A3>
struct Function<R __stdcall(A1, A2,A3)> :Function<R __stdcall(A1,A2)>
{typedef A3 ARG3;
};//如果是函数指针类型,可以用以下模板特化组
template <typename R>
struct Function<R(__stdcall*)()> {typedef R RESULT;
};template <typename R, typename A1>
struct Function<R(__stdcall*)(A1)> :Function<R(__stdcall*)()>
{typedef A1 ARG1;
};template <typename R, typename A1, typename A2>
struct Function<R(__stdcall*)(A1,A2)> :Function<R(__stdcall*)(A1)>
{typedef A2 ARG2;
};template <typename R, typename A1, typename A2,typename A3>
struct Function<R(__stdcall*)(A1,A2,A3)> :Function<R(__stdcall*)(A1,A2)>
{typedef A3 ARG3;
};// 如果有更多参数,可在此处扩展

测试代码:

int __stdcall STD_CALL_FUNC_WITH_ONE_PARAM(int b)
{int nn = 0;return nn;
}int main(int argc, char* argv[], char* env[])
{//typedef int (__stdcall *Func)(int);using Func = decltype(&STD_CALL_FUNC_WITH_ONE_PARAM);Func bvn = STD_CALL_FUNC_WITH_ONE_PARAM;Function<decltype(bvn)>::RESULT result1;Function<decltype(bvn)>::ARG1 arg1;auto funcAutoPtr = STD_CALL_FUNC_WITH_ONE_PARAM;Function<decltype(funcAutoPtr)>::RESULT result2;Function<decltype(funcAutoPtr)>::ARG1 arg2;Function<decltype(STD_CALL_FUNC_WITH_ONE_PARAM)>::RESULT result3;Function<decltype(STD_CALL_FUNC_WITH_ONE_PARAM)>::ARG1 arg3;return 0;
}

http://www.lryc.cn/news/211514.html

相关文章:

  • Ubuntu 23.10安装TeXlive并安装CTEX中文支持
  • SpringBoot中CommandLineRunner详解(含源码)
  • 通信基础(一):数据传输基础
  • 故障诊断模型 | Maltab实现BiLSTM双向长短期记忆神经网络故障诊断
  • 物联网和互联网医院小程序:如何实现医疗设备的远程监测和管理?
  • sharepoint2016-2019升级到sharepoint订阅版
  • CTFHub | MySQL流量、Redis流量、MongoDB流量的WriteUp
  • NSS刷题 js前端修改 os.path.join漏洞
  • ArcGIS Maps SDK for JS:隐藏地图边框
  • 带你秒懂MySQL!! 一万字详细知识点和基础操作 欢迎评论区怼我 (三)
  • kubeadmin部署k8s1.27.4
  • 【Aurix Tricore】HighTec启动代码crt0-tc37x.c分析笔记
  • Linux高级命令(扩展)
  • LLM在text2sql上的应用 | 京东云技术团队
  • 【MySQL】 复合查询 | 内外连接
  • 【linux】麒麟v10安装openjdk8
  • 项目部署与上线
  • 系统架构主题之八:非功能性需求对系统架构及设计的影响
  • 盛元广通化工实验室管理系统
  • 代码没注释?一个方法几百行?
  • Angular-04:指令
  • [SpringCloud] Eureka 与 Ribbon 简介
  • 【Python 零基础入门】常用内置函数 再探
  • 10.30二叉树一些性质,找公共祖先(一般与搜索树),操作的复杂度,选择题细节
  • 亮氨酸脯氨酸肽——一种新型的医药中间体研究肽
  • Ubuntu 22.04 开机闪logo后卡在/dev/sda3: clean
  • avue-crud 自定义列
  • 达索系统SOLIDWORKS 2024 装配体新增功能
  • 电脑入门:电脑专业英语500词,供新手参考
  • 采购管理工具-采购软件-Leangoo免费看板工具