当前位置:网站首页>Differences between auto and decltype inference methods (learning notes)
Differences between auto and decltype inference methods (learning notes)
2022-04-23 05:27:00 【What’smean】
auto and decltype There are three main differences :
First of all :auto The type specifier uses the compiler to calculate the initial value of a variable to infer its type , and decltype Although it also allows the compiler to analyze the expression and get its type , But it doesn't actually evaluate the value of the expression .
second : The compiler infers auto The type is sometimes not exactly the same as the type of the initial value , The compiler will change the result type appropriately to make it more consistent with the initialization rules . for example ,auto The top floor is usually ignored const, And put the bottom const Keep it . By contrast ,decltype The top level of the variable is retained const.
Third : And auto Different ,decltype The result type of is closely related to the expression form , If you add a pair of parentheses to the variable name , The type obtained will be different from that without parentheses . If decltype It's a variable without parentheses , The result is the type of the variable ; If you add one or more parentheses to a variable , Then the compiler will infer the reference type .
Code parsing :
#include<iostream>
#include<typeinfo>
using namespace std;
int main(){
int a = 3;
auto c1 = a;
decltype(a) c2 = a;
decltype((a)) c3 = a;
const int d = 5;
auto f1 = d;
decltype(d) f2 = d;
cou<<typeid(c1).name()<<endl;
cou<<typeid(c2).name()<<endl;
cou<<typeid(c3).name()<<endl;
cou<<typeid(f1).name()<<endl;
cou<<typeid(f2).name()<<endl;
c1++;
c2++;
c3++;
c3++;
f1++; //auto Ignore the top level const, So no mistake
f2++; // Report errors :f2 Is an integer constant , You can't add
cout<<a<<" "<<c1<<" "<<c2<<" "<<c3<<" "<<f1<<" "<<f2<<endl;
return 0;
}
For the first set of type inference ,a Is a non constant integer ,c1 The inference result is an integer ,c2 The inference result of is also an integer ,c3 The inference result is due to the variable a An extra pair of parentheses is added, so it is an integer reference .c1、c2、c3 Perform the auto increment operation in turn , because c3 It's a variable. a Another name for , therefore c3 Self increase is equivalent to a Self increasing , Final a、c1、c2、c3 The value of is changed to 4.
For the second set of type inference ,d Is a constant integer , Containing top layer const, Use auto The inferred type automatically ignores the top level const, therefore f1 The inference result is an integer ;decltype Keep the top layer const, therefore f2 The inference result is an integer constant .f1 The auto increment operation can be performed normally , And constant f2 The value of cannot be changed , So it can't increase itself .
版权声明
本文为[What’smean]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220544124364.html
边栏推荐
- Redis的基本知识
- (11) Vscode code formatting configuration
- Multiple mainstream SQL queries only take the latest one of the data
- Solution of how to log in with mobile phone verification code in wireless network
- Click the Add button - a box appears (similar to adding learning experience - undergraduate - Graduate)
- Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
- String class understanding - final is immutable
- egg中的多进程模型--egg文档搬运工
- Branch and loop statements
- Necessity of selenium preloading cookies
猜你喜欢
C# ,类库
When is it appropriate for automated testing? (bottom)
领域驱动模型DDD(三)——使用Saga管理事务
Use of uniapp native plug-ins
狼叔来找翻译人员了--plato--持续翻译中.....
Blender程序化地形制作
Master-slave replication of MariaDB database
How to add beautiful code blocks in word | a very complete method to sort out and compare
what is wifi6?
Laravel [view]
随机推荐
2021-09-27
d.ts---更详细的知识还是需要看官网的介绍(声明文件章节)
(11) Vscode code formatting configuration
MFC implementation resources are implemented separately by DLL
Devops life cycle, all you want to know is here!
[untitled]
d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
Parsing of string class intern() method
The title bar will be pushed to coincide with the status bar
JS time format conversion
Use pagoda + Xdebug + vscode to debug code remotely
What are the most popular recruitment technical skills in 2022? You can't think of it
2021-11-08
varnish入门
Double click The jar package cannot run the solution
Implementation of resnet-34 CNN with kears
JSP -- Introduction to JSP
egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~
Ehcache Memcache redis three caches
2021-11-01