当前位置:网站首页>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 .
边栏推荐
- 容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
- Kill -9 system call used by PID to kill process
- Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
- TCP packet sticking problem
- ASEMI整流桥DB207的导通时间与参数选择
- Optimization of middle alignment of loading style of device player in easycvr electronic map
- 面试突击63:MySQL 中如何去重?
- Jerry's updated equipment resource document [chapter]
- J'aimerais dire quelques mots de plus sur ce problème de communication...
- 微信小程序获取手机号
猜你喜欢

30 分钟看懂 PCA 主成分分析

Introduction to the usage of model view delegate principal-agent mechanism in QT

中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障

EasyCVR电子地图中设备播放器loading样式的居中对齐优化

The easycvr authorization expiration page cannot be logged in. How to solve it?

在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?

Unity particle special effects series - treasure chest of shining stars

【Swoole系列2.1】先把Swoole跑起来

10 advanced concepts that must be understood in learning SQL

Manifest of SAP ui5 framework json
随机推荐
Four processes of program operation
Why should Li Shufu personally take charge of building mobile phones?
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
Markdown grammar - better blogging
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
编译原理——自上而下分析与递归下降分析构造(笔记)
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
OpenEuler 会长久吗
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Appium automated test scroll and drag_ and_ Drop slides according to element position
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
TCP packet sticking problem
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
Mysqlimport imports data files into the database
IP, subnet mask, gateway, default gateway
Optimization of middle alignment of loading style of device player in easycvr electronic map
Easy introduction to SQL (1): addition, deletion, modification and simple query
1700C - Helping the Nature
Insert dial file of Jerry's watch [chapter]