当前位置:网站首页>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…))”代码存在的意义。
边栏推荐
- JMeter interface test response data garbled
- In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
- Remote code execution penetration test - B module test
- Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
- [rapid environment construction] openharmony 10 minute tutorial (cub pie)
- The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
- Nodejs 开发者路线图 2022 零基础学习指南
- 面试突击63:MySQL 中如何去重?
- Hongmeng introduction and development environment construction
- There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
猜你喜欢

李書福為何要親自掛帥造手機?

Interview assault 63: how to remove duplication in MySQL?

分布式不来点网关都说不过去

Summary of Android interview questions of Dachang in 2022 (II) (including answers)

How to solve the error "press any to exit" when deploying multiple easycvr on one server?

EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?

Unity particle special effects series - treasure chest of shining stars

Sqoop I have everything you want

Unity小技巧 - 绘制瞄准准心

IP, subnet mask, gateway, default gateway
随机推荐
Mysqlimport imports data files into the database
BearPi-HM_ Nano development environment
Jielizhi obtains the currently used dial information [chapter]
Run xv6 system
Getting started with pytest ----- test case rules
IP, subnet mask, gateway, default gateway
基于STM32+华为云IOT设计的智能路灯
Optimization of middle alignment of loading style of device player in easycvr electronic map
Interview assault 63: how to remove duplication in MySQL?
Unity粒子特效系列-闪星星的宝箱
Sqoop I have everything you want
Spark calculation operator and some small details in liunx
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
【Android】Kotlin代码编写规范化文档
JMeter interface test response data garbled
Basic configuration and use of spark
Kernel link script parsing
MySQL stored procedure
RepPoints:可形变卷积的进阶
Summary of Android interview questions of Dachang in 2022 (II) (including answers)