C + + Auto type descriptor _c language

Source: Internet
Author: User

Programming often requires the value of an expression to be assigned to a variable, which requires that the type of expression be clearly known when declaring a variable. But it's not always easy to do this, and sometimes it's not even possible. To solve this problem, the C++11 standard introduces the auto type specifier, which allows the compiler to parse the type that the expression belongs to.

Unlike the original descriptor, which corresponds to only one particular type, auto lets the compiler extrapolate the variable type by initial value. Obviously, the variable that the auto defines must have an initial value.

Using auto has the following advantages:

Reliability: It works if the type of expression changes, including when the function return value changes.
Performance: Make sure that no conversion will be made.
Usability: You don't have to worry about spelling difficulties and spelling errors in type names.
Efficiency: code can become more efficient.

Auto item = val1 + Val2; The result of the addition of VAL1 and Val2 infers the type of item
Auto i=0, *p = &i;/I is an integer, p is an integral pointer

Use Auto to declare multiple variables in a single statement. However, a declaration statement can have only one basic data type, so the initial basic data types for all variables in the statement must be consistent:

Auto Sz = 0, pi = 3.14; error!

The type of auto that the compiler infers is sometimes not exactly the same as the type of the initial value, and the compiler will appropriately change the result type to conform to the initialization rules, for example:

Using auto Deletes references

int count = ten;
int& countref = count;
Auto Myauto = countref;

Countref = one;
cout << Count << ""; Print one

Myauto =;
cout << Count << Endl; Print 11

You might think Myauto is an int reference, but it's not. It is just an int because the output is 11 11 instead of 11 12, and this can occur if auto has not deleted the reference.

Const qualifier
First introduce a statement: The top const indicates that the pointer itself is a constant, and the underlying const indicates that the object the pointer refers to is a constant. General Auto ignores the top const, while the underlying const is retained, for example:

int i = 0;
const int CI = i, &cr = CI;
Auto B = ci; B is an integer (the top-level const attribute of CI is ignored)
auto C = CR;//C is an integer (CR is the alias of CI, CI itself is a top-level const)
auto D = &i;/d is an integer pointer Address is a pointer to an integer)
auto e = &ci;/E is a pointer to an integer constant (the address to a constant object is a low-level const)

If the type of auto you want to infer is a top-level const, you need to specify that:

Const auto F = CI; The deduction type of CI is int,f is the const int type

You can also set the reference type to auto, at which point the original initialization rule still applies:

Auto &g = CI; G is an integer constant reference, bound to the CI
auto &h =;//Error: Cannot reference bound literal value
Const Auto &J = N.//OK: You can reference bound literals for constants

Remember that symbols * and & only belong to a declaration, not part of the base data type, so the initial value must be of the same type:

Auto k = ci, &l = i;  K is an integer, L is an integral reference to
auto &m = ci, *p = &ci;/M is a reference to an integer constant, p is a pointer to an integer constant of
auto &n = i, *p2 = &ci;//Error: The type of i is int, and the type of &CI is the const int

Attach more Sample code:

The following declaration is equivalent. In the first statement, declare the variable j as type int. In the second statement, the variable k is deduced to type int because the initialization expression (0) is an integer

int j = 0; Variable j is explicitly type int.
Auto k = 0; Variable k is implicitly type int because 0 are an integer.

The following declaration is equivalent, but the second declaration is simpler than the first. One of the most compelling reasons to use the Auto keyword is simple

Map<int,list<string>>::iterator i = M.begin (); 
Auto i = M.begin (); 

When using ITER and elem to start a cycle

#include <deque>
using namespace std;

int main ()
{
  deque<double> dqdoubledata (0.1);

  for (Auto iter = Dqdoubledata.begin (); Iter!= dqdoubledata.end (); ++iter)
  {/* ... *

  ///prefer range-for loops With the following information in mind
  //(this applies to any range-for with auto, not just deque) for

  (auto Ele M:dqdoubledata)//copies elements, not much better than the previous examples
  {/* ... */} for

  (auto& el Em:dqdoubledata)//observes and/or modifies elements in-place
  {/* ... */} for

  (const auto& Elem:dqdou Bledata)//observes elements in-place
  {/* ... */}
}

The following code fragment declares a pointer using the new operator and a pointer declaration

Double x = 12.34;
Auto *y = new Auto (x), **z = new Auto (&x);

The next code fragment declares multiple symbols in each declaration statement. Note that all the symbols in each statement will resolve to the same type.

Auto x = 1, *y = &x, **z = &y; resolves to int.
Auto A (2.01), *b (&a);     resolves to double.
Auto C = ' A ', *d (&c);     resolves to char.
Auto m = 1, &n = m;      resolves to int.

This snippet uses the conditional operator (?:) to declare the variable x as an integer with a value of 200:

int v1 = m, v2 =;
Auto x = v1 > v2? V1:v2;

The following code fragment initializes the variable x to the type int, initializes the variable y to a reference to the type const int, and initializes the variable FP to a pointer to a function that returns the type int.

int f (int x) {return x;}
int main ()
{
  Auto x = f (0);
  Const Auto & y = f (1);
  Int (*p) (int x);
  p = f;
  Auto FP = p;
  //...
}

Related Article

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.