当前位置:网站首页>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 .
边栏推荐
- STM32 key state machine 2 - state simplification and long press function addition
- Distinguish between basic disk and dynamic disk RAID disk redundant array
- The solution that flutterweb browser cannot be rolled back after refreshing
- The easycvr authorization expiration page cannot be logged in. How to solve it?
- 1700C - Helping the Nature
- JMeter interface test response data garbled
- C语言通过指针交换两个数
- 一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- declval(指导函数返回值范例)
猜你喜欢

ASEMI整流桥DB207的导通时间与参数选择

Is it meaningful for 8-bit MCU to run RTOS?

Zen integration nails, bugs, needs, etc. are reminded by nails

重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...

SAP UI5 框架的 manifest.json

Manifest of SAP ui5 framework json
![[Android] kotlin code writing standardization document](/img/d5/53d6a75e87af15799bf7e5d6eb92a5.png)
[Android] kotlin code writing standardization document

队列的实现

Optimization of middle alignment of loading style of device player in easycvr electronic map

编译原理——自上而下分析与递归下降分析构造(笔记)
随机推荐
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
Reppoints: advanced order of deformable convolution
Open source and safe "song of ice and fire"
李書福為何要親自掛帥造手機?
Shell input a string of numbers to determine whether it is a mobile phone number
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
Interesting - questions about undefined
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
d绑定函数
MySQL stored procedure
基于STM32+华为云IOT设计的智能路灯
Appium automated test scroll and drag_ and_ Drop slides according to element position
Alibaba brand data bank: introduction to the most complete data bank
模板于泛型编程之declval
Manifest of SAP ui5 framework json
10 advanced concepts that must be understood in learning SQL
STM32按键状态机2——状态简化与增加长按功能
微信小程序获取手机号
Unity particle special effects series - treasure chest of shining stars
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing