C++11 the use of the new feature auto

Source: Internet
Author: User
Objective

C + + is a strongly typed language that must be explicitly identified when declaring a variable. However, in practice, it is difficult to infer the type of value of an expression, especially as the template type appears, making it more difficult to understand the return type of some complex expression. To solve this problem, 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.

First, 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.

#include <vector> #include <map>   using namespace std;   int main (int argc, char *argv[], char *env[]) {//auto A;  Error, no initialization expression, cannot infer the type of a//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.    //1. Auto Help deduction type  auto a = ten;  Auto C = ' A ';  Auto S ("Hello");    2. Type lengthy  map<int, map<int,int> > map_;  Map<int, Map<int,int>>::const_iterator itr1 = Map_.begin ();  Const Auto ITR2 = Map_.begin ();  Auto ptr = [] ()  {  std::cout << "Hello World" << Std::endl;  };    return 0; };   3. When using template technology, if the type of a variable depends on the template parameter,//Do not use auto will be difficult to determine the type of the variable (after using auto, will be determined automatically by the compiler). Template <class T, class u> void Multiply (T T, u u) {  Auto V = T * u;}

Second, the return value of the placeholder

Template <typename T1, typename t2> Auto Compose (T1 T1, T2 T2), decltype (t1 + T2) {  return t1+t2;} Auto V = Compose (2, 3.14); V ' s type is double

Third, the use of precautions

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

Auto k = 5;auto* PK = new auto (k); auto** PpK = new Auto (&k); const auto n = 6;

2. Variables declared with auto must be initialized

Auto m; M should be intialized

3. Auto cannot be combined with other types

auto int p; This is the practice of old auto.

4. Function and template parameters cannot be declared as auto

void MyFunction (auto parameter) {}//No auto as method argument  Template<auto t>//utter nonsense-not allowed void Fun (T-t) {}

5. Variables defined on the heap, expressions using auto must be initialized

int* p = new Auto (0); fineint* pp = new auto (); should be initialized  auto x = new Auto (),//hmmm ... no intializer  auto* y = new auto (9);//Fine. Here y is a int*auto z = new Auto (9); Fine. Here Z is a int* (It isn't just an int)

6, because auto is a placeholder, not a type of his own, so it cannot be used for type conversion or some other operations, such as sizeof and typeid

int value = 123;auto x2 = (auto) value; No casting using auto  Auto x3 = static_cast<auto> (value);//Same as above

7. Variables defined in an auto sequence must always be deduced into the same type

Auto X1 = 5, x2 = 5.0, x3= ' R '; This is too much....we cannot combine

8. Auto cannot be automatically deduced into Cv-qualifiers (constant & volatile qualifiers) unless it is declared as a reference type

const int i = 99;auto J = i; j is int, rather than const INTJ = +//Fine. As j is not constant  //Now let us try to has referenceauto& k = i;//now k is const INT&K = +;//Error. K is constant  //similarly with volatile qualifer

9, Auto will degenerate exponentially pointer to array, unless declared as a reference

int A[9];auto j = A;cout<<typeid (j). Name () <<endl; This would print int*  auto& k = A;cout<<typeid (k). Name () <<endl;//This would print int [9]

Summarize

The above is the entire content of this article, I hope that the content of this article for everyone to learn or use C + + can have a certain help, if there are questions you can message exchange.

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.