C++ auto新特性

原文地址:http://blog.csdn.net/huang_xw/article/details/8760403

C++11中引入的auto主要有兩種用途:自動類型推斷和返回值佔位。auto在C++98中的標識臨時變量的語義,由於使用極少且多餘,在C++11中已被刪除。前後兩個標準的auto,完全是兩個概念。

1. 自動類型推斷

    auto自動類型推斷,用於從初始化表達式中推斷出變量的數據類型。通過auto的自動類型推斷,可以大大簡化我們的編程工作。下面是一些使用auto的例子。
  1. #include <vector>  
  2. #include <map>  
  3.   
  4. using namespace std;  
  5.   
  6. int main(int argc, char *argv[], char *env[])  
  7. {  
  8. //  auto a;                 // 錯誤,沒有初始化表達式,無法推斷出a的類型  
  9. //  auto int a = 10;        // 錯誤,auto臨時變量的語義在C++11中已不存在, 這是舊標準的用法。  
  10.   
  11.     // 1. 自動幫助推導類型  
  12.     auto a = 10;  
  13.     auto c = 'A';  
  14.     auto s("hello");  
  15.   
  16.     // 2. 類型冗長  
  17.     map<int, map<int,int> > map_;  
  18.     map<int, map<int,int>>::const_iterator itr1 = map_.begin();  
  19.     const auto itr2 = map_.begin();  
  20.     auto ptr = []()  
  21.     {  
  22.         std::cout << "hello world" << std::endl;  
  23.     };  
  24.   
  25.     return 0;  
  26. };  
  27.   
  28. // 3. 使用模板技術時,如果某個變量的類型依賴於模板參數,  
  29. // 不使用auto將很難確定變量的類型(使用auto後,將由編譯器自動進行確定)。  
  30. template <class T, class U>  
  31. void Multiply(T t, U u)  
  32. {  
  33.     auto v = t * u;  
  34. }  

2. 返回值佔位

  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.使用注意事項

①我們可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 來修飾auto
  1. auto k = 5;  
  2. auto* pK = new auto(k);  
  3. auto** ppK = new auto(&k);  
  4. const auto n = 6;  
②用auto聲明的變量必須初始化
  1. auto m; // m should be intialized    
③auto不能與其他類型組合連用
  1. auto int p; // 這是舊auto的做法。  
④函數和模板參數不能被聲明爲auto
  1. void MyFunction(auto parameter){} // no auto as method argument  
  2.   
  3. template<auto T> // utter nonsense - not allowed  
  4. void Fun(T t){}  
⑤定義在堆上的變量,使用了auto的表達式必須被初始化
  1. int* p = new auto(0); //fine  
  2. int* pp = new auto(); // should be initialized  
  3.    
  4. auto x = new auto(); // Hmmm ... no intializer  
  5.      
  6. auto* y = new auto(9); // Fine. Here y is a int*  
  7. auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)  
⑥以爲auto是一個佔位符,並不是一個他自己的類型,因此不能用於類型轉換或其他一些操作,如sizeof和typeid
  1. int value = 123;  
  2. auto x2 = (auto)value; // no casting using auto  
  3.   
  4. auto x3 = static_cast<auto>(value); // same as above   
⑦定義在一個auto序列的變量必須始終推導成同一類型
  1. auto x1 = 5, x2 = 5.0, x3='r';  // This is too much....we cannot combine like this  
⑧auto不能自動推導成CV-qualifiers(constant & volatile qualifiers),除非被聲明爲引用類型
  1. const int i = 99;  
  2. auto j = i;       // j is int, rather than const int  
  3. j = 100           // Fine. As j is not constant  
  4.   
  5. // Now let us try to have reference  
  6. auto& k = i;      // Now k is const int&  
  7. k = 100;          // Error. k is constant  
  8.   
  9. // Similarly with volatile qualifer  
⑨auto會退化成指向數組的指針,除非被聲明爲引用
  1. int a[9];  
  2. auto j = a;  
  3. cout<<typeid(j).name()<<endl; // This will print int*  
  4.   
  5. auto& k = a;  
  6. cout<<typeid(k).name()<<endl; // This will print int [9]  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章