The use of the new feature--auto "C++11"

Source: Internet
Author: User

Transfer from http://blog.csdn.net/huang_xw/article/details/8760403

The auto introduced in C++11 has two main purposes: automatic type inference and return-value placeholder. The semantics of the identity temp variable in auto in C++98, which has been removed in c++11 because of its minimal use and redundancy. Before and after two standard auto, is completely two concepts.

1. Automatic type inference

Auto automatic type inference, which is used to infer the data type of a variable from an initialization expression. Automatic type inference from auto can greatly simplify our programming efforts. Here are some examples of using auto.

[CPP]View Plaincopyprint?
  1. #include <vector>
  2. #include <map>
  3. Using namespace std;
  4. int main (int argc, char *argv[], char *env[])
  5. {
  6. Auto A; Error, no initialization expression, cannot infer type of a
  7. auto int a = 10; Error, the semantics of the auto temp variable no longer exist in c++11, which is the use of the old standard.
  8. //1. Automatic Help deduction type
  9. Auto A = 10;
  10. Auto C = ' A ';
  11. Auto S ("Hello");
  12. //2. Lengthy type
  13. map<int, map<int,int> > Map_;
  14. map<int, map<int,int>>::const_iterator itr1 = Map_.begin ();
  15. Const Auto ITR2 = Map_.begin ();
  16. Auto ptr = [] ()
  17. {
  18. Std::cout << "Hello World" << Std::endl;
  19. };
  20. return 0;
  21. };
  22. 3. When using template technology, if the type of a variable depends on the template parameter,
  23. It is difficult to determine the type of a variable without using auto (the compiler will automatically determine it after using auto).
  24. Template <class T, class u>
  25. void Multiply (T T, u u)
  26. {
  27. Auto V = t * u;
  28. }
2. Return-Value Placeholder

[CPP]View Plaincopyprint?
    1. Template <TypeName T1, TypeName t2>
    2. Auto Compose (T1 T1, T2 T2), decltype (t1 + T2)
    3. {
    4. return t1+t2;
    5. }
    6. Auto V = Compose (2, 3.14); //V ' s type is double

3. Precautions for use

① we can use Valatile,pointer (*), reference (&), rvalue Reference (&&) to decorate auto

[CPP]View Plaincopyprint?
    1. Auto k = 5;
    2. auto* PK = new Auto (k);
    3. auto** PpK = new Auto (&k);
    4. Const Auto n = 6;

② a variable declared with auto must be initialized

[CPP]View Plaincopyprint?
    1. Auto m; //M should be intialized

③auto cannot be combined with other types of combinations

[CPP]View Plaincopyprint?
    1. Auto int p;  //This is the practice of old auto.

④ function and template parameters cannot be declared as auto

[CPP]View Plaincopyprint?
    1. void MyFunction (auto parameter) {} //No auto as method argument
    2. Template<auto t> //utter nonsense-not allowed
    3. void Fun (T-t) {}

⑤ a variable defined on a heap, an expression that uses auto must be initialized

[CPP]View Plaincopyprint?
  1. int* p = New auto (0); //fine
  2. int* pp = new auto (); //should be initialized
  3. Auto x = new Auto (); //hmmm ... no intializer
  4. auto* y = new auto (9); //Fine. Here y is a int*
  5. Auto z = New auto (9); //fine. Here Z is a int* (It isn't just an int)

⑥ that auto is a placeholder, not a type of his own, and therefore cannot be used for type conversions or other operations such as sizeof and typeid

[CPP]View Plaincopyprint?
    1. int value = 123;
    2. Auto x2 = (auto) value; //No casting using auto
    3. Auto x3 = static_cast<auto> (value); //Same as above

⑦ a variable defined in an auto sequence must always be deduced into the same type

[CPP]View Plaincopyprint?
    1. Auto X1 = 5, x2 = 5.0, x3=' R '; //This is too much....we cannot combine

⑧auto cannot be automatically deduced as Cv-qualifiers (constant & volatile qualifiers) unless declared as a reference type

[CPP]View Plaincopyprint?
  1. const INT i = 99;
  2. Auto J = i; //j is int, rather than const int
  3. j = + //Fine. As J is not constant
  4. Now let us try to have reference
  5. auto& k = i; //Now K-is const int&
  6. k = 100; //Error. K is constant
  7. Similarly with volatile qualifer

⑨auto will degenerate exponentially pointers to arrays, unless declared as references

[CPP]View Plaincopyprint?
      1. int a[9];
      2. Auto J = A;
      3. cout<<typeID (j). Name () <<endl; //This'll print int*
      4. auto& k = A;
      5. cout<<typeid (k). Name () <<endl; //This would print int [9]

The use of the new feature--auto "C++11"

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.