C + + Learning Note 2--constexpr, type alias, auto

Source: Internet
Author: User

---restore content starts---

CONSTEXPR: The modifier constant expression, the contract adornment object not only is the constant, but also can obtain the value in the compilation stage.

Unlike const, CONSTEXPR is bound by the parties, that is, constexpr int *a=1; A pointer itself is a const, and the right is simple enough, and a const int*a; Such a statement contracts A to the object is a const , this is a different point. The constexpr is directly limited to the top-level const.

Type aliases: traditional typedef,

Double1.1;

Code like this means A is a double type

The new C11 standard has a new type alias definition:

Using a = double;

There is a point to note about the type aliases of pointers:

Char *A; Const 0;//cstr is a constant pointer to char Const a *p; P is a pointer to a char constant pointer

If the direct literal substitution is understood, the typedef will get a wrong understanding.

For example, bring in the following code: const char *a = 0; This is the underlying const, and this understanding is wrong.

Auto Type: C11 the type introduced by the new standard. The compiler helps us to determine the type.

Similar to auto a = 1; A is an int type.

It is important to note that:

Const int *a ==constint1= c;

b The result is a pointer to an int constant, and d is just the normal int type.

The compiler discards the top-level const, while the underlying const is preserved.

Similar to:

1, b=2;

Such code, you must ensure that a, and B are the same type.

Otherwise you will get an error.

Decltype Type Indicator

Another indicator of the c++11 new standard is the Decltype type indicator:

Decltype (f ()) sum = x;

It qualifies the type according to the F () return value, which, unlike the top and bottom const, differs from auto, and the const is retained, even the top level.

Such as:

const int i = 0;

Decltype (i) a = 0;

A is also a const int.

The dereference and () expressions will also return a reference at the end.

---restore content ends---

C + + Learning Note 2--constexpr, type alias, auto

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.