当前位置:网站首页>Declval of template in generic programming
Declval of template in generic programming
2022-07-06 18:03:00 【Hair like snow ty】
std::declval yes c++11 A function template that appears in the standard , This function template looks strange , Because it has no function body ( It didn't come true , Only the statement , It was designed on purpose ), So it can't be called , It is generally used for decltype,sizeof And other keywords are used together for class type derivation 、 Occupy memory space calculation, etc . Its source code is as follows :
template<typename T>
add_ravalue_reference_t<T> declval() noexcept;
add_ravalue_reference( Note that there is no _t,add_ravalue_reference_t Is the alias template corresponding to this type ) yes c++ A class template provided in the standard library , Its function is to introduce a type , Return the right value reference type of this type . Such as :
(1) Pass in int type , The return is int && type ;
(2) Pass in int & type , Then it returns int &, The reference folding rule is applied here .
(3) Pass in int && type , Back again int && type , The reference folding rule is also used here .
You can see it in the source code declval The function of is to return a certain type T To the right of , Whether or not the type has a default constructor , Or whether this type can create objects . These actions are completed at compile time , So also known as std::declval Is a compile time tool .
class cTest
{
public:
cTest(int a)
{
cout << " Constructors \n";
}
double myfunc()
{
cout << "myfunc\n";
return 1.1;
}
};
problem : How to get myfunc() The return type of this member function double? The traditional approach is as follows :
int main()
{
cTest myobj(1);
cout << typeid(decltype(myobj.myfunc())).name() << endl;
system("pause");
return 0;
}
result :
It can be seen from the results , In order to obtain myfunc The return type of , You must create a class cTest The object of ; that , You can think more deeply , Create a class myobj so much trouble , Also provide arguments , If you don't create objects of classes , Still want myfunc() The return type of the member function , Can this idea be realized ? Certainly. ~
int main()
{
cout << typeid(decltype(declval<cTest>().myfunc())).name() << endl;
system("pause");
return 0;
}
result :
From the results , There are no objects that create classes , No call myfunc Member functions , But I really got myfunc The return type of .
declval Function summary :
(1) From the perspective of type conversion , Convert any type to an R-value reference type .
(2) From the perspective of hypothetical creation of certain types of objects , coordination decltype, Make decltype This type does not have to be passed in the expression ( Generally refers to a class type ) Constructor for , You can use the member functions of this class .
Be careful ,std::declval Cannot be called , Nor can you create any objects . however std::declval The ability to create objects without , To achieve the effect of creating an object of this type .
边栏推荐
- 酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
- Jerry's updated equipment resource document [chapter]
- Zen integration nails, bugs, needs, etc. are reminded by nails
- 2022年大厂Android面试题汇总(一)(含答案)
- 1700C - Helping the Nature
- Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
- MSF horizontal MSF port forwarding + routing table +socks5+proxychains
- 2019阿里集群数据集使用总结
- J'aimerais dire quelques mots de plus sur ce problème de communication...
- Insert dial file of Jerry's watch [chapter]
猜你喜欢
OpenCV中如何使用滚动条动态调整参数
QT中Model-View-Delegate委托代理机制用法介绍
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
2019阿里集群数据集使用总结
Open source and safe "song of ice and fire"
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
ASEMI整流桥DB207的导通时间与参数选择
Unity小技巧 - 绘制瞄准准心
随机推荐
2019阿里集群数据集使用总结
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
MySQL stored procedure
Sqoop I have everything you want
Debug and run the first xv6 program
Open source and safe "song of ice and fire"
1700C - Helping the Nature
2022年大厂Android面试题汇总(二)(含答案)
Unity particle special effects series - treasure chest of shining stars
Wechat applet obtains mobile number
Jerry's watch deletes the existing dial file [chapter]
Alibaba brand data bank: introduction to the most complete data bank
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Why should Li Shufu personally take charge of building mobile phones?
【.NET CORE】 请求长度过长报错解决方案
MSF horizontal MSF port forwarding + routing table +socks5+proxychains
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
8位MCU跑RTOS有没有意义?
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day