当前位置:网站首页>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…))”代码存在的意义。
边栏推荐
- FlutterWeb浏览器刷新后无法回退的解决方案
- Compile and build, from the bottom to the top
- MarkDown语法——更好地写博客
- Reppoints: advanced order of deformable convolution
- 重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
- Jerry's updated equipment resource document [chapter]
- The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- The art of Engineering (1): try to package things that do not need to be exposed
- Insert dial file of Jerry's watch [chapter]
猜你喜欢
重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
RepPoints:可形变卷积的进阶
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
EasyCVR授权到期页面无法登录,该如何解决?
Optimization of middle alignment of loading style of device player in easycvr electronic map
Is it meaningful for 8-bit MCU to run RTOS?
随机推荐
Olivetin can safely run shell commands on Web pages (Part 1)
Compile and build, from the bottom to the top
node の SQLite
It doesn't make sense without a distributed gateway
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
二分(整数二分、实数二分)
2022年大厂Android面试题汇总(二)(含答案)
How to use scroll bars to dynamically adjust parameters in opencv
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Spark calculation operator and some small details in liunx
面试突击63:MySQL 中如何去重?
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
Pourquoi Li shufu a - t - il construit son téléphone portable?
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
趣-关于undefined的问题
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
高精度运算
基本磁盘与动态磁盘 RAID磁盘冗余阵列区分