C++11 的到來又爲C++注入了新鮮的血液,更嚴格的類型檢查,新的功能特性以及新收錄的STL,都使得C++11更爲安全和高效。筆者將C++11的學習過程記錄下來,不對之處還望指正。
IDE: vs2012
1.簡介
auto是C++早就具備的關鍵字(局部變量默認定義爲auto類型),但是C++11中,auto被賦予了新的含義 —— 定義任意類型
2.根據初始化推導類型
auto 可以根據初始化來自動推導類型,例如:
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- class TestClass
- {
- public:
- TestClass():a(0),b(0){}
- int a;
- int b;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- auto i = 2;
- auto j = 3.4f;
- auto k = 4.0;
- TestClass tc;
- auto obj = tc;
- cout<<"Type of i : " << typeid(i).name() << endl;
- cout<<"Type of j : " << typeid(j).name() << endl;
- cout<<"Type of k : " << typeid(k).name() << endl;
- cout<<"Type of obj : " << typeid(obj).name() << endl;
- return 0;
- }
輸出結果如下圖所示:
可以看出,auto 關鍵字定義的變量,編譯器會根據初始化操作自動確定其類型。注意,對於浮點型,如果不以 f 標記結尾則默認爲double數據類型。這種自動類型的推導,可以簡化代碼,例如,在STL Container的使用過程中,經常需要定義 iterator 來對容器的數據進行操作,如下所示:
- vector< list< SomeType> > testVec;
- vector< list< SomeType> >::iterator iter = testVec.begin();
- auto iter = testVec.begin();
3. 其它限定詞 (const , *, & 等 )
auto 還可以和其它的限定詞一起使用,例如 const ,指針, 取址等,如下例所示
- #include<iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- const auto n1 = 1;
- auto const n2 = 2;
- auto n3 = 3;
- const int n4 = 4;
- auto p1 = new int(4);
- auto* p2 = new int(5);
- //compile error!!
- //n1 = 11;
- //compile error!!
- //n2 = 22;
- cout<<"Type of n1 : " << typeid( n1 ).name() << endl;
- cout<<"Type of n2 : " << typeid( n2 ).name() << endl;
- cout<<"Type of n3 : " << typeid( n3 ).name() << endl;
- cout<<"Type of n4 : " << typeid( n4 ).name() << endl;
- cout<<"Type of p1 : " << typeid( p1 ).name() << endl;
- cout<<"Type of p2 : " << typeid( p2 ).name() << endl;
- return 0;
- }
輸出結果如下圖所示:
由輸出結果可以看出,使用const對auto類型變量限定後, 該變量即爲常量類型,不可修改(對於const類型,輸出類型沒有const)。同時也可以看出,使用指針對auto進行限定和不使用指針,變量的類型都相同,例如 p1 和 p2 , 類型由編譯器自動推導。
對於const類型賦值,auto 並不會自動推導出const限定,必須使用引用,例如
- const int i=1;
- auto j = i; // j is int
- auto& k = i; // k is const int
4. 解決特殊問題
在使用模板進行泛型編程時,有些時候類型難以確定,這時 auto 關鍵字可以發揮重要作用(配合 decltype,後面講述 ),例如
- #include<iostream>
- using namespace std;
- template< class T1, class T2 >
- auto Fun( T1 t1, T2 t2 ) -> decltype( t1*t2 )
- {
- return t1 * t2;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- auto nRes = Fun( 2, 3 );
- auto fRes = Fun( 2.0f, 4 );
- auto dRes = Fun( 2.0, 5 );
- cout<<"Type of nRes : " << typeid( nRes ).name() << endl;
- cout<<"Type of fRes : " << typeid( fRes ).name() << endl;
- cout<<"Type of dRes : " << typeid( dRes ).name() << endl;
- return 0;
- }
其輸出結果如下圖所示
對於函數 Fun, 兩個不同的參數類型會導致最終的結果類型不確定,如果通過函數重載來實現該功能,會書寫大量的代碼,通過模板, auto , 和 decltype 可以輕鬆實現對類型的推導,將類型的決定權交給編譯器!
5. 注意事項
auto 的使用非常方便,但也並非任何場合都使用,需要遵守一定的規則
- #include<iostream>
- using namespace std;
- //1. Compile Error: error C3533: 'auto': a parameter cannot have a type that contains 'auto'
- void fun(auto i)
- {
- return;
- }
- //2. Compile Error: error C3533: 'auto': a parameter cannot have a type that contains 'auto
- template< auto T >
- void fun2() { return ;}
- int _tmain(int argc, _TCHAR* argv[])
- {
- //3. Compile Error: error C3531: 'i': a symbol whose type contains 'auto' must have an initializer.
- auto i;
- //4. Compile Error: error C3530: 'auto' cannot be combined with any other type-specifier
- auto double d = 2.9;
- //5. Compile Error: error C3537: 'auto': you cannot cast to a type that contains 'auto'
- int j = 8;
- auto jj = (auto)j;
- return 0;
- }
(1)auto 不能作爲函數參數
(2)auto 不能作爲模板類型參數
(3)auto 定義的變量必須進行初始化
(4)auto 不能與其它類型一起使用
(5)不能使用auto進行類型轉換
轉載自http://blog.csdn.net/fire_lord/article/details/8499596