一文詳解C++11中auto的使用
在C語(yǔ)言中,就有了auto關(guān)鍵字,它被當(dāng)作是一個(gè)變量的存儲(chǔ)類型修飾符,表示自動(dòng)變量(局部變量)。它不能被單獨(dú)使用,否則編譯器會(huì)給出警告。在C++11標(biāo)準(zhǔn)中,添加了新的類型推導(dǎo)特性。在C ++11中,使用auto定義的變量不能使用其它類型修飾符修飾,該變量的類型由編譯器根據(jù)初始化數(shù)據(jù)自動(dòng)確定。
C++中類型檢查是在編譯階段。動(dòng)態(tài)類型語(yǔ)言能做到在運(yùn)行時(shí)決定類型,主要?dú)w功于一技術(shù),這技術(shù)是類型推導(dǎo)。在C++11中,可以通過(guò)重定義auto關(guān)鍵字來(lái)實(shí)現(xiàn)類型推導(dǎo)。
在C++11中,使用auto關(guān)鍵字可以要求編譯器對(duì)變量的類型進(jìn)行自動(dòng)推導(dǎo)。
auto關(guān)鍵字:類型推導(dǎo),從該關(guān)鍵字的初始化表達(dá)式中推導(dǎo)變量的類型。
在塊作用域、命名空間作用域、for循環(huán)的初始化語(yǔ)句內(nèi)聲明變量的時(shí)候,變量的類型可以被省略,使用關(guān)鍵字auto來(lái)代替。
auto聲明的變量必須被初始化,以使編譯器能夠從其初始化表達(dá)式中推導(dǎo)出其類型。
聲明為auto的變量在編譯時(shí)期就分配了內(nèi)存,而不是到了運(yùn)行時(shí)期,所以使用auto不再引發(fā)任何速度延遲,這也意味著使用auto的時(shí)候,這個(gè)變量不初始化會(huì)報(bào)錯(cuò),因?yàn)榫幾g器無(wú)法知道這個(gè)變量的類型。
auto使用時(shí)需注意:
(1)、可以使用const、volatile、pointer(*)、reference(&)、rvalue reference(&&)等說(shuō)明符和聲明符來(lái)修飾auto關(guān)鍵字;
(2)、用auto聲明的變量必須初始化;
(3)、auto不能與其它任何類型說(shuō)明符一起使用;
(4)、方法、參數(shù)或模板參數(shù)不能被聲明為auto;
(5)、定義在堆上的變量,使用了auto的表達(dá)式必須被初始化;
(6)、auto是一個(gè)占位符,不是類型,不能用于類型轉(zhuǎn)換或其它一些操作,如sizeof、typeid;
(7)、auto關(guān)鍵字內(nèi)聲明的聲明符列表的所有符號(hào)必須解析為同一類型;
(8)、auto不能自動(dòng)推導(dǎo)成CV-qualifiers(constant& volatile qualifiers),除非被聲明為引用類型;
(9)、auto會(huì)退化成指向數(shù)組的指針,除非被聲明為引用;
(10)、auto不能作為函數(shù)的返回類型,在C++14中是可以的。
建議:大多數(shù)情況使用關(guān)鍵字auto,除非非常需要轉(zhuǎn)換。
下面是從其他文章中copy的測(cè)試代碼,詳細(xì)內(nèi)容介紹可以參考對(duì)應(yīng)的reference:
#include "auto.hpp" #include <iostream> #include <cmath> #include <typeinfo> #include <string> #include <map> #include <list> #include <deque> #include <vector> // // reference: http://en.cppreference.com/w/cpp/language/auto template<class T, class U> auto add(T t, U u) -> decltype(t + u) // the return type is the type of operator+(T, U) { return t + u; } auto get_fun(int arg) -> double(*)(double) // same as: double (*get_fun(int))(double) { switch (arg) { case 1: return std::fabs; case 2: return std::sin; default: return std::cos; } } int test_auto1() { auto a = 1 + 2; std::cout << "type of a: " << typeid(a).name() << '\n'; // type of a: int auto b = add(1, 1.2); std::cout << "type of b: " << typeid(b).name() << '\n'; // type of b: double auto c = { 1, 2 }; std::cout << "type of c: " << typeid(c).name() << '\n'; // type of c: class std::initializer_list<int> auto my_lambda = [](int x) { return x + 3; }; std::cout << "my_lambda: " << my_lambda(5) << '\n'; // my_lambda: 8 auto my_fun = get_fun(2); std::cout << "type of my_fun: " << typeid(my_fun).name() << '\n'; // type of my_fun: double (__cdecl*)(double) std::cout << "my_fun: " << my_fun(3) << '\n'; // my_fun: 0.14112 // auto int x; // error as of C++11: "auto" is no longer a storage-class specifier // error C3530: “auto”不能與任何其他類型說(shuō)明符組合 return 0; } // reference: https://msdn.microsoft.com/zh-cn/library/dd293667(v=vs.120).aspx int f(int x) { return x; } int test_auto2() { int count = 10; int& countRef = count; auto myAuto = countRef; countRef = 11; std::cout << count << " " << std::endl; // 11 myAuto = 12; std::cout << count << std::endl; // 11 // 1. 下面的聲明等效。 在第一個(gè)語(yǔ)句中,聲明 j 變量為類型 int。 在第二個(gè)語(yǔ)句,因?yàn)槌跏蓟磉_(dá)式 (0) 是整數(shù),所以變量 k 推導(dǎo)為 int 類型 int j = 0; // Variable j is explicitly type int. auto k = 0; // Variable k is implicitly type int because 0 is an integer. // 2. 以下聲明等效,但第二個(gè)聲明比第一個(gè)簡(jiǎn)單 std::map<int, std::list<std::string>> m; std::map<int, std::list<std::string>>::iterator i = m.begin(); auto i_ = m.begin(); // 3. 聲明 iter 和 elem 變量類型 std::deque<double> dqDoubleData(10, 0.1); for (auto iter = dqDoubleData.begin(); iter != dqDoubleData.end(); ++iter) { /* ... */} // prefer range-for loops with the following information in mind // (this applies to any range-for with auto, not just deque) for (auto elem : dqDoubleData) // COPIES elements, not much better than the previous examples { /* ... */ } for (auto& elem : dqDoubleData) // observes and/or modifies elements IN-PLACE { /* ... */ } for (const auto& elem : dqDoubleData) // observes elements IN-PLACE { /* ... */ } // 4. 使用 new 運(yùn)算符 double x = 12.34; auto *y = new auto(x), **z = new auto(&x); // 5. 所有符號(hào)解析為同一類型 auto x_ = 1, *y_ = &x_, **z_ = &y_; // Resolves to int. auto a(2.01), *b(&a); // Resolves to double. auto c = 'a', *d(&c); // Resolves to char. auto m_ = 1, &n_ = m_; // Resolves to int. // 6. 使用條件運(yùn)算符 (?:) int v1 = 100, v2 = 200; auto e = v1 > v2 ? v1 : v2; // 7. 將變量 x7 初始化類型 int,將引用的變量 y7 初始化為類型 const int,及將變量 fp 初始化為指向返回類型 int 的函數(shù)的指針 auto x7 = f(0); const auto & y7 = f(1); int(*p)(int x7); p = f; auto fp = p; return 0; } / // reference: http://www.learncpp.com/cpp-tutorial/4-8-the-auto-keyword/ int add_3(int x, int y) { return x + y; } int test_auto3() { auto d = 5.0; // 5.0 is a double literal, so d will be type double auto i = 1 + 2; // 1 + 2 evaluates to an integer, so i will be type int auto sum = add_3(5, 6); // add_3() returns an int, so sum will be type int return 0; }
GitHub:https://github.com/fengbingchun/Messy_Test
到此這篇關(guān)于一文詳解C++11中auto的使用的文章就介紹到這了,更多相關(guān)C++11 auto內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng)的源碼分享
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07如何利用C語(yǔ)言實(shí)現(xiàn)最簡(jiǎn)單的HTTP服務(wù)器詳解
這篇文章主要給大家介紹了關(guān)于如何利用C語(yǔ)言實(shí)現(xiàn)最簡(jiǎn)單的HTTP服務(wù)器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C語(yǔ)言具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11C語(yǔ)言實(shí)現(xiàn)C++繼承和多態(tài)的代碼分享
本文主要給大家簡(jiǎn)單講訴了C和C++的區(qū)別以及如何使用C語(yǔ)言模擬實(shí)現(xiàn)C++繼承和多態(tài),并附上示例代碼,是篇相當(dāng)不錯(cuò)的文章,推薦給喜歡C語(yǔ)言的小伙伴們2017-07-07C++類中三大函數(shù)詳解(構(gòu)造、析構(gòu)和拷貝)
c++三大函數(shù)指的是拷貝構(gòu)造、拷貝賦值、析構(gòu)函數(shù),下面這篇文章主要給大家介紹了關(guān)于C++類中三大函數(shù)(構(gòu)造、析構(gòu)和拷貝)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03Qt+OpenCV利用幀差法實(shí)現(xiàn)車輛識(shí)別
所謂幀差法也就是對(duì)連續(xù)圖像幀做差分運(yùn)算,其結(jié)果與定義好的閾值比較,若大于閾值則為運(yùn)動(dòng)目標(biāo)值為1,否則值為0?。本文將利用幀差法實(shí)現(xiàn)車輛識(shí)別,感興趣的可以了解一下2022-08-08