当前位置:网站首页>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 .
边栏推荐
- 重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
- Easy introduction to SQL (1): addition, deletion, modification and simple query
- 最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
- Codeforces Round #803 (Div. 2)
- Unity粒子特效系列-闪星星的宝箱
- Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
- Pytest learning ----- pytest confitest of interface automation test Py file details
- d绑定函数
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- The difference between parallelism and concurrency
猜你喜欢
Sqoop I have everything you want
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Pytest learning ----- pytest confitest of interface automation test Py file details
微信为什么使用 SQLite 保存聊天记录?
OliveTin能在网页上安全运行shell命令(上)
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
李書福為何要親自掛帥造手機?
There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
随机推荐
Pytest learning ----- pytest confitest of interface automation test Py file details
Codeforces Round #803 (Div. 2)
酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
How to use scroll bars to dynamically adjust parameters in opencv
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
Jielizhi obtains the currently used dial information [chapter]
C语言通过指针交换两个数
Dichotomy (integer dichotomy, real dichotomy)
F200——搭载基于模型设计的国产开源飞控系统无人机
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
Interview shock 62: what are the precautions for group by?
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Wechat applet obtains mobile number
Interview shock 62: what are the precautions for group by?
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升