当前位置:网站首页>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 .
边栏推荐
- RepPoints:可形变卷积的进阶
- FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
- Interview shock 62: what are the precautions for group by?
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
- I want to say more about this communication failure
- Pytorch extract middle layer features?
- adb常用命令
- 中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
- STM32 key state machine 2 - state simplification and long press function addition
- 【Android】Kotlin代码编写规范化文档
猜你喜欢
8位MCU跑RTOS有没有意义?
【Android】Kotlin代码编写规范化文档
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
Pourquoi Li shufu a - t - il construit son téléphone portable?
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
SAP UI5 框架的 manifest.json
declval(指导函数返回值范例)
Interview shock 62: what are the precautions for group by?
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
随机推荐
DNS hijacking
adb常用命令
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
OliveTin能在网页上安全运行shell命令(上)
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅
C语言通过指针交换两个数
OpenCV中如何使用滚动条动态调整参数
1700C - Helping the Nature
Pourquoi Li shufu a - t - il construit son téléphone portable?
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
李书福为何要亲自挂帅造手机?
Pytest learning ----- detailed explanation of the request for interface automation test
Codeforces Round #803 (Div. 2)
RepPoints:可形变卷积的进阶
Getting started with pytest ----- test case pre post, firmware
【Android】Kotlin代码编写规范化文档
编译原理——自上而下分析与递归下降分析构造(笔记)
2022年大厂Android面试题汇总(一)(含答案)
8位MCU跑RTOS有没有意义?