当前位置:网站首页>declval(指导函数返回值范例)
declval(指导函数返回值范例)
2022-07-06 10:05:00 【发如雪-ty】
有如下大代码:
int myfunc(int a, int b)
{
return a + b;
}
template<typename T_F,typename...U_Args>
decltype(declval<T_F>() (declval<U_Args>()...)) testImpl(T_F func, U_Args...args)
{
return func(args...);
}
int main()
{
int nSum = testImpl(myfunc, 13, 15);
cout << nSum << endl;
system("pause");
return 0;
}
运行结果:
我们都知道函数类型是由返回值类型和参数类型决定的,与函数名没有关系,所以这里的myfunc的函数类型为int(int,int),前面的代表的是返回值类型,括号中的是两个参数类型。
上述代码有一段推导函数模板返回值类型的代码比较有意思。从main函数中可以看到,testImpl并没有指定模板参数,所以模板参数是由编译器推断的。下面将推断类型做一个简单的解释。
(1)T_F被推断为int(*)(int,int)
(2)U_Args代表一包类型,传入的是5,8,所以会被推导为int,int.
(3)值得注意的是decltype(declval<T_F>() (declval<U_Args>()...))
,这种写法,我第一次接触的时候有点不习惯,但只要搞清楚它的作用就行了:根据函数类型以及参数类型推导出函数的返回值类型。
其实,这里还有一种不使用std::declval也能实现同样功能的写法-返回类型后置。实现起来要比std::declval清爽很多。
template<typename T_F, typename...U_Args>
template<typename T_F, typename...U_Args>
auto testImpl_2(T_F func, U_Args...args)->decltype(func(args...))
{
return func(args...);
}
int main()
{
int nSum = testImpl_2(myfunc, 13, 15);
cout << nSum << endl;
system("pause");
return 0;
}
结果:28
可能有的同学,会去掉->decltype(func(args...))
,因为这样也能得到正确的答案,但这不是一个好主意,testImpl_2的本意是希望返回的类型与第一个类型模板参数T_F所代表的类型(既myfunc()函数返回类型)完全一样,而这正是“->decltype(func(args…))”代码存在的意义。
边栏推荐
- C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
- QT中Model-View-Delegate委托代理机制用法介绍
- IP, subnet mask, gateway, default gateway
- Getting started with pytest ----- allow generate report
- 开源与安全的“冰与火之歌”
- Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
- Run xv6 system
- Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
- [rapid environment construction] openharmony 10 minute tutorial (cub pie)
- Insert dial file of Jerry's watch [chapter]
猜你喜欢
Pyspark operator processing spatial data full parsing (5): how to use spatial operation interface in pyspark
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
C语言通过指针交换两个数
Is it meaningful for 8-bit MCU to run RTOS?
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
PyTorch 提取中间层特征?
Spark calculation operator and some small details in liunx
分布式不来点网关都说不过去
Pytest learning ----- detailed explanation of the request for interface automation test
Easy introduction to SQL (1): addition, deletion, modification and simple query
随机推荐
The art of Engineering (1): try to package things that do not need to be exposed
QT中Model-View-Delegate委托代理机制用法介绍
Spark calculation operator and some small details in liunx
微信小程序中给event对象传递数据
Is it meaningful for 8-bit MCU to run RTOS?
Jerry's watch deletes the existing dial file [chapter]
Alibaba brand data bank: introduction to the most complete data bank
Open source and safe "song of ice and fire"
TCP packet sticking problem
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
关于这次通信故障,我想多说几句…
分布式不来点网关都说不过去
Growth of operation and maintenance Xiaobai - week 7
FlutterWeb浏览器刷新后无法回退的解决方案
Wechat applet obtains mobile number
adb常用命令
Nodejs 开发者路线图 2022 零基础学习指南
The art of Engineering
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)