当前位置:网站首页>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 .
边栏推荐
- 队列的实现
- Shell input a string of numbers to determine whether it is a mobile phone number
- 编译原理——预测表C语言实现
- Optimization of middle alignment of loading style of device player in easycvr electronic map
- Sqoop I have everything you want
- Jerry's access to additional information on the dial [article]
- 2022年大厂Android面试题汇总(二)(含答案)
- It doesn't make sense without a distributed gateway
- Four processes of program operation
- 偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
猜你喜欢

JMeter interface test response data garbled

Four processes of program operation

Smart street lamp based on stm32+ Huawei cloud IOT design

STM32 key state machine 2 - state simplification and long press function addition

Compilation principle - top-down analysis and recursive descent analysis construction (notes)

RB157-ASEMI整流桥RB157

偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”

Unity小技巧 - 绘制瞄准准心

基本磁盘与动态磁盘 RAID磁盘冗余阵列区分

8位MCU跑RTOS有没有意义?
随机推荐
關於這次通信故障,我想多說幾句…
【Android】Kotlin代码编写规范化文档
Compilation principle - top-down analysis and recursive descent analysis construction (notes)
Jerry's updated equipment resource document [chapter]
30 分钟看懂 PCA 主成分分析
d绑定函数
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
2022年大厂Android面试题汇总(二)(含答案)
Jerry's watch deletes the existing dial file [chapter]
ASEMI整流桥DB207的导通时间与参数选择
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
The easycvr authorization expiration page cannot be logged in. How to solve it?
Nodejs 开发者路线图 2022 零基础学习指南
OpenCV中如何使用滚动条动态调整参数
In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
Pytest learning ----- pytest confitest of interface automation test Py file details
面试突击62:group by 有哪些注意事项?
Jerry's access to additional information on the dial [article]
MySQL stored procedure
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)