


The difference and connection between auto and decltype in the new features of C++11
Jan 23, 2017 pm 02:34 PMThe difference and connection between auto and decltype in the new features of C++11
1. Introduction to auto
When programming, it is often necessary to pay the value of an expression to a variable. This needs to be done in When you declare a variable, you know exactly what type it is. However, doing this is not that easy (especially in templates), and sometimes it is not possible at all. In order to solve this problem, the new C++11 standard introduces the auto type specifier, which allows the compiler to analyze the type of the expression for us. It is different from the original ones that only correspond to a specific type specifier (such as int). auto lets the compiler perform type deduction through initial values. Thus, the type of the defined variable is obtained, so the variable defined by auto must have an initial value.
1 2 |
|
The type of item here is calculated by the compiler by adding the types of val_1 and val_2 during the compilation process. If it is val_1(int) + val_2(double), then the type of item is double. Data type, so the initial basic data type of all variables of the rain gear must be the same. Be sure to distinguish between data types and type modifiers here! !
1 2 3 |
|
This First, as we know, the use of references is actually a reference object. In particular, when the reference is used as the initial value, the real participation is actually the value of the reference object. At this time, the compiler uses the type of the reference object as the type of auto:
1 2 |
|
1 2 3 4 5 6 |
|
1 |
|
1 2 3 |
|
1 |
|
Basically, the function of decltype is very similar to that of auto, so I won’t list them one by one. Another use for decltype is the postfix return type introduced in c++11.
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Another important difference between decltype and auto is that the result type of decltype is closely related to the expression form. There is one situation that requires special attention: for the expression used by decltype, if a pair of parentheses is added to the variable name, the type obtained may be different from that without parentheses. If decltype uses a variable without parentheses, the result is the type of the variable. But if you add one or more layers of parentheses to this variable, the compiler will treat this variable as an expression. The variable is a special expression that can be used as an lvalue, so such a decltype will return the reference type:
1 2 3 4 |
|
这里再指出一个需要注意的地方就是 = 赋值运算符返回的是左值的引用。换句话意思就是说 decltype(i = b) 返回类型为 i 类型的引用。仔细看下面这段代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
运行结果为:
1 2 3 4 5 |
|
由上面的代码和运行结果可以看出来,1.decltype(i = 41)中的赋值语句并没有真正的运行。2. decltype(i = 41)返回的其实是int&,也就是说x 其实是 i 的引用。
了解了auto 和 decltype后,以后在使用的过程中一定要分清两者的区别,防止在定义的时候产生const 与非const 以及引用 与非引用 的差别!!
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
更多C++11新特性中auto 和 decltype 区别和联系相关文章请关注PHP中文网!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the alternatives to NULL in C language

Is NULL still important in modern programming in C language?

Method of copying code by C language compiler

What are the web versions of C language compilers?

C language online programming website C language compiler official website summary

C language compiler installation tutorial (computer version)
