当前位置:网站首页>declval(指导函数返回值范例)
declval(指导函数返回值范例)
2022-07-06 10:05:00 【发如雪-ty】
有如下大代码:
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;
}
运行结果:
我们都知道函数类型是由返回值类型和参数类型决定的,与函数名没有关系,所以这里的myfunc的函数类型为int(int,int),前面的代表的是返回值类型,括号中的是两个参数类型。
上述代码有一段推导函数模板返回值类型的代码比较有意思。从main函数中可以看到,testImpl并没有指定模板参数,所以模板参数是由编译器推断的。下面将推断类型做一个简单的解释。
(1)T_F被推断为int(*)(int,int)
(2)U_Args代表一包类型,传入的是5,8,所以会被推导为int,int.
(3)值得注意的是decltype(declval<T_F>() (declval<U_Args>()...)),这种写法,我第一次接触的时候有点不习惯,但只要搞清楚它的作用就行了:根据函数类型以及参数类型推导出函数的返回值类型。
其实,这里还有一种不使用std::declval也能实现同样功能的写法-返回类型后置。实现起来要比std::declval清爽很多。
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;
}
结果:28
可能有的同学,会去掉->decltype(func(args...)),因为这样也能得到正确的答案,但这不是一个好主意,testImpl_2的本意是希望返回的类型与第一个类型模板参数T_F所代表的类型(既myfunc()函数返回类型)完全一样,而这正是“->decltype(func(args…))”代码存在的意义。
边栏推荐
- 78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
- 1700C - Helping the Nature
- The easycvr authorization expiration page cannot be logged in. How to solve it?
- Mysqlimport imports data files into the database
- JMeter interface test response data garbled
- How to use scroll bars to dynamically adjust parameters in opencv
- Spark calculation operator and some small details in liunx
- FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
- Olivetin can safely run shell commands on Web pages (Part 1)
- EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
猜你喜欢

OliveTin能在网页上安全运行shell命令(上)

78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO

Spark accumulator and broadcast variables and beginners of sparksql
![[introduction to MySQL] the first sentence · first time in the](/img/1e/2401d821b1008922bc5f8a15de31f9.png)
[introduction to MySQL] the first sentence · first time in the "database" Mainland

Four processes of program operation

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

Alibaba brand data bank: introduction to the most complete data bank

Getting started with pytest ----- allow generate report

1700C - Helping the Nature

F200——搭载基于模型设计的国产开源飞控系统无人机
随机推荐
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
Jielizhi obtains the currently used dial information [chapter]
Unity tips - draw aiming Center
It doesn't make sense without a distributed gateway
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
二分(整数二分、实数二分)
Getting started with pytest ----- test case pre post, firmware
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
Wechat applet obtains mobile number
[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
高精度运算
Kernel link script parsing
Pytest learning ----- pytest confitest of interface automation test Py file details
Getting started with pytest ----- allow generate report
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
IP, subnet mask, gateway, default gateway
The easycvr authorization expiration page cannot be logged in. How to solve it?
Debug and run the first xv6 program