当前位置:网站首页>Declval (example of return value of guidance function)
Declval (example of return value of guidance function)
2022-07-06 18:04:00 【Hair like snow ty】
There are the following large codes :
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;
}
Running results :
We all know that the function type is determined by the return value type and parameter type , It has nothing to do with the function name , So here myfunc The function type of is int(int,int), The previous represents the return value type , In parentheses are two parameter types .
The above code has an interesting code for deriving the return value type of function template . from main You can see in the function ,testImpl Template parameters are not specified , So the template parameters are inferred by the compiler . Here is a simple explanation of inference types .
(1)T_F To be inferred as int(*)(int,int)
(2)U_Args Represents a package type , What's coming in is 5,8, So it will be deduced as int,int.
(3) It is worth noting that decltype(declval<T_F>() (declval<U_Args>()...))
, This kind of writing , I was not used to it when I first came into contact , But just figure out its function : Deduce the return value type of the function according to the function type and parameter type .
Actually , There's another way not to use std::declval It can also achieve the same function - Return type postposition . It's more practical than std::declval It's so refreshing .
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;
}
result :28
Maybe some students , Will take out ->decltype(func(args...))
, Because this can also get the right answer , But it's not a good idea ,testImpl_2 The original meaning of is the type you want to return and the first type template parameter T_F Type represented ( Both myfunc() Function return type ) Exactly the same as , And that's what “->decltype(func(args…))” The meaning of code .
边栏推荐
- adb常用命令
- Jerry's watch reads the file through the file name [chapter]
- sql语句优化,order by desc速度优化
- JMeter interface test response data garbled
- Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
- Pytorch extract middle layer features?
- Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- Alibaba brand data bank: introduction to the most complete data bank
- Interesting - questions about undefined
- OliveTin能在网页上安全运行shell命令(上)
猜你喜欢
Jerry's updated equipment resource document [chapter]
传统家装有落差,VR全景家装让你体验新房落成效果
RepPoints:可形变卷积的进阶
酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
2022年大厂Android面试题汇总(二)(含答案)
李書福為何要親自掛帥造手機?
The solution that flutterweb browser cannot be rolled back after refreshing
随机推荐
Unity小技巧 - 绘制瞄准准心
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
微信小程序获取手机号
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
【Android】Kotlin代码编写规范化文档
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
Mysqlimport imports data files into the database
MSF横向之MSF端口转发+路由表+SOCKS5+proxychains
Reppoints: advanced order of deformable convolution
Awk command exercise
adb常用命令
FlutterWeb浏览器刷新后无法回退的解决方案
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Insert dial file of Jerry's watch [chapter]
EasyCVR授权到期页面无法登录,该如何解决?
Markdown grammar - better blogging
Interview shock 62: what are the precautions for group by?