C + + auto and Decltype

Source: Internet
Author: User

There are two new methods of c++11, Auto and Decltype, which make our variable definitions much more convenient.

The Auto keyword is deduced according to the type of its initialization.

(1) Ignoring the Const property of the initializer itself when torn down. Such as:

int x=0; Auto a=x,b=0; Cout<<typeid (a). Name () << "\ T" <<typeid (b). Name () <<endl;

Output:

I i (I represent int type)
(2) If it is a pointer or reference, retain the const attribute of its referent. Such as:

const int *p=0; int *q = 0; auto px = p; Auto QX = q; Cout<<typeid (px). Name () << "\ T" <<typeid (QX). Name () <<endl
const int &RX = X;auto &MR = Rx; Mr is cited as a reference to the const int type ++MR; Error

Output:

PKi (pointer to const int type) Pi (pointer to int type)
(3) As with the general definition statement, multiple variables can be defined in a definition statement, and the types can be different, but must have the same basic type. Such as:

int X=0;auto Ax=x,*xp=&ax,**xxp=&xp;cout<<typeid (ax). Name () << "\ T" <<typeid (XP). Name () <<typeid (XXP). Name () <<endl;
Output:

I Pi PPi (pointer to int pointer), the basic type of the three is int

const int Ci=x;auto *px=&x,*pci = &ci;  Error, there is &x to get the base type int, while the &ci is pushed down to get the base type const INT
(4) The array type is pushed to the pointer type, and the function type is pushed down to the pointer type that points to the function. such as:

int f (int i) {}int Iar[100];auto Aiar = Iar;auto af = F;cout<<typeid (IAR). Name () << "\ T" <<typeid (Aiar). Name () <<endl;cout<<typeid (f). Name () << "\ T" <<typeid (AF). Name () <<endl;

Output:

A100_i Pi (pointing to an int array of length 100 to a pointer of type int)

Fiie Pfiie (argument is int, the function that returns int is a pointer to a function that has a parameter of int and returns a value of int)

(5) The reference type pushes down the object of the type to which it is bound. Such as:

int X=0;int &m = X;auto am = m;
++m;
cout<<x<< "\ t" <<m<< "\ T" <<am<<endl;++am; cout<<x<< "\ t" <<m<< "\ T" <<am<<endl;

Output:

1 10

1 11

(6) When using the auto definition, you can add the const modifier to it, just like a normal definition

int x=0;const int &m = X,*PX = &x;  M and PX refer to the reference and pointer to the const int cout<<x<< "\ t" <<m<< "\ T" <<*px<<endl; output:0  0   0 ++x cout<<x<< "\ t" <<m<< "\ T" <<*px<<endl;  Output:1  1   1++m;  Error ++*px;  Error

Decltype is torn down according to the provided object, but does not use it as an initializer.

(1) the Const property of the initializer itself is preserved when pushed down, and the literal value is pushed down to the variable. such as:

int x=0; const int z=0; Decltype (x) dx=100; Decltype (0) dy = 200; Decltype (z) DZ = 300; ++DX; ++dy; cout<<dx<< "\ T" <<dy<<endl; OUTPUT:101 201 ++dz;  Error, DZ was pushed down to const type

(2) A pointer or reference to the type of a const attribute is reserved. such as:

  int x;  const int *p = &x;  Decltype (p) DP = &x;  Pull down to get pointer to const INT type  CONST int &RX = x;  Decltype (RX) Drx = x; Tear down to get a reference  ///++ (*p) bound to the const int type;  ++rx;

(3) As with the general definition statement, multiple variables can be defined in a definition statement, and the types can be different, but must have the same basic type.

Such as:

int X;decltype (x) *px=&x,&rx=x,m=x;

(4) array and function type keep the original type, do not do pointer conversion. such as:

int f (int i) {}
Decltype (f) df;int arr[100];d ecltype (arr) Dar;cout<<typeid (DF). Name () << "\ T" <<typeid (arr). Name () <<endl;///output:fiie  a100_i
(6) When the expression for Decltype is provided as an lvalue, the type of the expression is pushed down to get the reference type of the expression and the right value. such as:

  int x=0;  int *p = &x;  Get int Reference type  ///decltype (*p) pt; Error, reference type must initialize  decltype (*p) PR = x;  ++PR;  cout<<x<< "\ t" <<pr<<endl;  Output:1   1  ////Because *p+0 is the right value, you can get *p type Object  decltype (*p + 0) RR by the following method;  Ok

C + + auto and Decltype

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.