[C ++ 11] new feature-auto usage

Source: Internet
Author: User

Auto introduced in C ++ 11 has two main purposes: automatic type inference and return value placeholder. The semantics of the Temporary Variable identified by auto in C ++ 98 has been deleted in C ++ 11 because it is rarely used and redundant. The first and second standard auto is completely two concepts.

1. Automatic type inference

Auto auto type inference is used to deduce the Data Type of the variable from the initialization expression. Auto's automatic type inference can greatly simplify our programming. Below are some examples of using auto.

# Include <vector> # include <map> using namespace STD; int main (INT argc, char * argv [], char * env []) {// auto; // error. No initialization expression is available. The type of a cannot be inferred. // auto int A = 10; // the syntax of the auto temporary variable does not exist in C ++ 11, this is the old standard usage. // 1. auto A = 10; 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 the template technology, if the type of a variable depends on the template parameter, // it is difficult to determine the type of the variable without auto (the compiler automatically determines the type of the variable after auto is used ). Template <class T, Class U> void multiply (t, u) {Auto V = T * U ;}
2. Return Value 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

 

3. Precautions

① We can use valatile, pointer (*), reference (&), Rvalue reference (&) to modify auto

auto k = 5;auto* pK = new auto(k);auto** ppK = new auto(&k);const auto n = 6;

② Variables declared with auto must be initialized

auto m; // m should be intialized  

③ Auto cannot be combined with other types

Auto int P; // This is the old auto practice.

④ Function and template parameters cannot be declared as auto

void MyFunction(auto parameter){} // no auto as method argumenttemplate<auto T> // utter nonsense - not allowedvoid Fun(T t){}

⑤ Variables defined on the heap must be initialized using the auto expression.

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 is not just an int)

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

int value = 123;auto x2 = (auto)value; // no casting using autoauto x3 = static_cast<auto>(value); // same as above 

7. variables defined in an auto sequence must always be deduced to the same type

auto x1 = 5, x2 = 5.0, x3='r';  // This is too much....we cannot combine like this

⑧ Auto cannot be automatically deduced to CV-qualifiers (constant & volatile qualifiers) unless declared as a reference type

const int i = 99;auto j = i;       // j is int, rather than const intj = 100           // Fine. As j is not constant// Now let us try to have referenceauto& k = i;      // Now k is const int&k = 100;          // Error. k is constant// Similarly with volatile qualifer

⑨ Auto degrades to a pointer to an array, unless declared as a reference

int a[9];auto j = a;cout<<typeid(j).name()<<endl; // This will print int*auto& k = a;cout<<typeid(k).name()<<endl; // This will print int [9]

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.