当前位置:网站首页>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 .
边栏推荐
- Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
- Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
- Summary of Android interview questions of Dachang in 2022 (II) (including answers)
- Manifest of SAP ui5 framework json
- Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
- Pytest learning ----- detailed explanation of the request for interface automation test
- Pytorch extract middle layer features?
- Getting started with pytest ----- test case rules
- 面试突击63:MySQL 中如何去重?
- 78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
猜你喜欢

The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?

1700C - Helping the Nature

8位MCU跑RTOS有没有意义?

QT中Model-View-Delegate委托代理机制用法介绍

Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing

Open source and safe "song of ice and fire"

2019阿里集群数据集使用总结

In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way

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

SQL statement optimization, order by desc speed optimization
随机推荐
SAP UI5 框架的 manifest.json
Kill -9 system call used by PID to kill process
Spark accumulator and broadcast variables and beginners of sparksql
模板于泛型编程之declval
JMeter interface test response data garbled
Optimization of middle alignment of loading style of device player in easycvr electronic map
std::true_type和std::false_type
VR panoramic wedding helps couples record romantic and beautiful scenes
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
I want to say more about this communication failure
Pytorch extract middle layer features?
编译原理——自上而下分析与递归下降分析构造(笔记)
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
Transfer data to event object in wechat applet
Jerry's watch reading setting status [chapter]
【Android】Kotlin代码编写规范化文档
RepPoints:可形变卷积的进阶
趣-关于undefined的问题
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅