当前位置:网站首页>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
边栏推荐
- Domain driven model DDD (III) -- using saga to manage transactions
- App Store年交易额100万美元只缴15%佣金,中小开发者心里很矛盾
- MFC implementation resources are implemented separately by DLL
- Edit, cancel, pull up menu
- 相机成像+单应性变换+相机标定+立体校正
- Source code analysis of how to use jump table in redis
- 2021-09-27
- 領域驅動模型DDD(三)——使用Saga管理事務
- Call the interface to get the time
- The source of anxiety of graduating college students looking for technology development jobs
猜你喜欢
Devops life cycle, all you want to know is here!
What are the most popular recruitment technical skills in 2022? You can't think of it
[untitled] Notepad content writing area
[untitled]
Low code and no code considerations
Project manager's thinking mode worth trying: project success equation
Kanban Quick Start Guide
!!!!!!!!!!!!!!!!!!
selenium預先加載cookie的必要性
Laravel routing settings
随机推荐
Five key technologies to improve the devsecops framework
Simple and basic use of switch and if
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]
The introduction of lean management needs to achieve these nine points in advance
egg测试的知识大全--mock、superTest、coffee
Implementation of resnet-34 CNN with kears
SQL Server检索SQL和用户信息的需求
Use pagoda + Xdebug + vscode to debug code remotely
JS time format conversion
C# ,类库
MySQL series - install MySQL 5.6.27 on Linux and solve common problems
[untitled] Notepad content writing area
[the background color changes after clicking a line]
How to add beautiful code blocks in word | a very complete method to sort out and compare
开源规则引擎——ice:致力于解决灵活繁复的硬编码问题
Devops life cycle, all you want to know is here!
String class understanding - final is immutable
Edit, cancel, pull up menu
Qingdao agile tour, coming!
Arithmetic and logical operations