C++11學習筆記(1) —— auto

C++11 的到來又爲C++注入了新鮮的血液,更嚴格的類型檢查,新的功能特性以及新收錄的STL,都使得C++11更爲安全和高效。筆者將C++11的學習過程記錄下來,不對之處還望指正。

IDE: vs2012


1.簡介

auto是C++早就具備的關鍵字(局部變量默認定義爲auto類型),但是C++11中,auto被賦予了新的含義 —— 定義任意類型


2.根據初始化推導類型

auto 可以根據初始化來自動推導類型,例如:

  1. #include "stdafx.h"  
  2. #include<iostream>  
  3. using namespace std;  
  4.   
  5. class TestClass  
  6. {  
  7. public:  
  8.     TestClass():a(0),b(0){}  
  9.   
  10.     int a;  
  11.     int b;  
  12. };  
  13.   
  14. int _tmain(int argc, _TCHAR* argv[])  
  15. {  
  16.     auto i = 2;  
  17.     auto j = 3.4f;  
  18.     auto k = 4.0;  
  19.   
  20.     TestClass tc;  
  21.     auto obj = tc;  
  22.   
  23.     cout<<"Type of i : " << typeid(i).name() << endl;  
  24.     cout<<"Type of j : " << typeid(j).name() << endl;  
  25.     cout<<"Type of k : " << typeid(k).name() << endl;  
  26.     cout<<"Type of obj : " << typeid(obj).name() << endl;  
  27.   
  28.     return 0;  
  29. }  


輸出結果如下圖所示:

可以看出,auto 關鍵字定義的變量,編譯器會根據初始化操作自動確定其類型。注意,對於浮點型,如果不以 f 標記結尾則默認爲double數據類型。這種自動類型的推導,可以簡化代碼,例如,在STL Container的使用過程中,經常需要定義 iterator 來對容器的數據進行操作,如下所示:

  1. vector< list< SomeType> >  testVec;  
  2. vector< list< SomeType> >::iterator iter = testVec.begin();  


通過使用 auto 可以簡化爲

  1. auto iter = testVec.begin();  


3. 其它限定詞 (const , *, & 等 )

auto 還可以和其它的限定詞一起使用,例如 const ,指針, 取址等,如下例所示

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int _tmain(int argc, _TCHAR* argv[])  
  5. {  
  6.     const auto n1 = 1;  
  7.     auto const n2 = 2;  
  8.     auto n3 = 3;  
  9.     const int n4 = 4;  
  10.     auto p1 = new int(4);  
  11.     auto* p2 = new int(5);  
  12.   
  13.     //compile error!!  
  14.     //n1 = 11;   
  15.   
  16.     //compile error!!  
  17.     //n2 = 22;  
  18.   
  19.     cout<<"Type of n1 : " << typeid( n1 ).name() << endl;  
  20.     cout<<"Type of n2 : " << typeid( n2 ).name() << endl;  
  21.     cout<<"Type of n3 : " << typeid( n3 ).name() << endl;  
  22.     cout<<"Type of n4 : " << typeid( n4 ).name() << endl;  
  23.     cout<<"Type of p1 : " << typeid( p1 ).name() << endl;  
  24.     cout<<"Type of p2 : " << typeid( p2 ).name() << endl;  
  25.   
  26.     return 0;  
  27. }  

輸出結果如下圖所示:


由輸出結果可以看出,使用const對auto類型變量限定後, 該變量即爲常量類型,不可修改(對於const類型,輸出類型沒有const)。同時也可以看出,使用指針對auto進行限定和不使用指針,變量的類型都相同,例如 p1 和 p2 , 類型由編譯器自動推導。


對於const類型賦值,auto 並不會自動推導出const限定,必須使用引用,例如

  1. const int i=1;  
  2. auto j = i;  // j is int  
  3. auto& k = i; // k is const int  


4. 解決特殊問題

在使用模板進行泛型編程時,有些時候類型難以確定,這時 auto 關鍵字可以發揮重要作用(配合 decltype,後面講述 ),例如

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. templateclass T1, class T2 >  
  5. auto Fun( T1 t1, T2 t2 ) -> decltype( t1*t2 )  
  6. {  
  7.     return t1 * t2;  
  8. }  
  9.   
  10. int _tmain(int argc, _TCHAR* argv[])  
  11. {  
  12.     auto nRes = Fun( 2, 3 );  
  13.     auto fRes = Fun( 2.0f, 4 );  
  14.     auto dRes = Fun( 2.0, 5 );  
  15.   
  16.     cout<<"Type of nRes : " << typeid( nRes ).name() << endl;  
  17.     cout<<"Type of fRes : " << typeid( fRes ).name() << endl;  
  18.     cout<<"Type of dRes : " << typeid( dRes ).name() << endl;  
  19.   
  20.     return 0;  
  21. }  

其輸出結果如下圖所示


對於函數 Fun, 兩個不同的參數類型會導致最終的結果類型不確定,如果通過函數重載來實現該功能,會書寫大量的代碼,通過模板, auto , 和 decltype 可以輕鬆實現對類型的推導,將類型的決定權交給編譯器!


5. 注意事項

auto 的使用非常方便,但也並非任何場合都使用,需要遵守一定的規則

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. //1. Compile Error: error C3533: 'auto': a parameter cannot have a type that contains 'auto'  
  5. void fun(auto i)  
  6. {  
  7.     return;  
  8. }  
  9.   
  10. //2. Compile Error: error C3533: 'auto': a parameter cannot have a type that contains 'auto  
  11. template< auto T >  
  12. void fun2() { return ;}  
  13.   
  14. int _tmain(int argc, _TCHAR* argv[])  
  15. {  
  16.     //3. Compile Error: error C3531: 'i': a symbol whose type contains 'auto' must have an initializer.  
  17.     auto i;  
  18.   
  19.     //4. Compile Error: error C3530: 'auto' cannot be combined with any other type-specifier  
  20.     auto double d = 2.9;  
  21.   
  22.     //5. Compile Error: error C3537: 'auto': you cannot cast to a type that contains 'auto'  
  23.     int j = 8;  
  24.     auto jj = (auto)j;  
  25.   
  26.     return 0;  
  27. }  

(1)auto 不能作爲函數參數

(2)auto 不能作爲模板類型參數

(3)auto 定義的變量必須進行初始化

(4)auto 不能與其它類型一起使用

(5)不能使用auto進行類型轉換

轉載自http://blog.csdn.net/fire_lord/article/details/8499596

發佈了21 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章