C + + 11 declaration

Source: Internet
Author: User

1. Auto

C++11 is used to implement automatic type inference. This requires a display initialization so that the compiler can set the type of the variable to the type of the initial value.

Auto Maton = 112; Maton is type int

Auto pt = &maton; PT is type int *

Double FM (double, int);

Auto PF = FM; PF is type double (*) (double, int)


Keyword Auto can also simplify template declarations. For example, if I1 is a Std::initializer_list<double> object, the following code can be used:

for (Std::initializer_list<double>::iterator p = i1.begin (); P! = I1.end (); p++)

Replace with the following code:

for (Auto P = i1.begin (); P! = I1.end (); p++)


2.decltype

The Dectype keyword declares the type of a variable as the type specified by an expression.

Decltype (x) y; X is an expression that makes Y the same type as x.

Double X;

int n;

Decltype (x*n) q; The type of q is the same as x*n, double

Decltype (&x) PD; The type of PD is the same as &x, double *

This is especially useful when defining a template, because the type can only be determined when the template is instantiated:

Template <typename T, TypeName u>

void ef (T T, u u)

{

Decltype (T*u) Tu;

...

}

The type of TU is the type of t*u, assuming that the operation T*u is defined, and if T is char,u for short, then TU will be int, which is caused by an integer elevation that is automatically performed by the shaping operation.


3. Template aliases: using =

For lengthy or complex identifiers, it is convenient to create their aliases. Previously, C + + provided a typedef for this purpose;

typedef std::vector<std::string>::iterator;

C++11 provides another syntax for creating aliases:

Using Ittype = std::vector<std::string>iterator;

The difference is that the new syntax can be used for template partial materialization, but typedef cannot:

Template<typename t>

Using Arr12 = std::array<t,12>;

The above statement materializes template array<t, int>, for example:

Std::array<double, 12> A1;

Std::array<std::string, 12> A2;

They can be replaced with the following declaration:

Arr12<double> A1;

Arr12<std::string> A2;


4. nullptr

A null pointer is a pointer that does not point to valid data. C++11 New keyword nullptr is used to represent a null pointer, which is a pointer type and cannot be converted to an integer type. For backwards compatibility, c++11 still allows the use of zero to represent a null pointer, so the expression nullptr = = 0 is true, but using nullptr instead of 0 provides a higher type safety. For example, you can pass 0 to a function that accepts an int parameter, but if you try to pass nullptr to such a function, the compiler treats this as an error. Therefore, for clarity and security reasons, use the nullptr ——— if the compiler supports it.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + 11 declaration

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.