C++教程之auto關鍵字的使用

來源:互聯網
上載者:User

標籤:

一、 auto關鍵字的前世

從C語言開始,auto關鍵字就被當作是一個變數的儲存類型修飾符,表示自動變數(局部變數)。它不能被單獨使用,否則編譯器會給出警告。

#include <stdio.h>int main(){        int a = 123;        auto int b = 234;        auto c = 345;        printf("a = %d, b = %d, c = %d\n", a, b, c);        return 0;}

編譯運行結果:

$ gcc main.cmain.c:7:7: warning: type specifier missing, defaults to ‘int‘ [-Wimplicit-int]        auto c = 345;        ~~~~ ^1 warning generated.$ ./a.out a = 123, b = 234, c = 345
二、 auto關鍵字的今生

在C++ 11標準中,添加了新的類型推導特性,考慮到auto關鍵字很少使用,就給它重新賦予了功能——申明類型由編譯器推導的變數。在C++ 11中,使用auto定義的變數不能使用其它類型修飾符修飾,該變數的類型由編譯器根據初始化資料自動確定。auto類型的變數必須進行初始化。

#include <iostream>int main(){        int a = 21;        auto b = a;        //typeid可以擷取變數或者資料類型的名字        std::cout << typeid(b).name() << std::endl;        return 0;}

1 .使用C++ 98標準進行編譯,會出現警告:

$ clang++ main.cpp -std=c++98main.cpp:6:2: warning: ‘auto‘ type specifier is a C++11 extension      [-Wc++11-extensions]        auto b = a;        ^1 warning generated.$ ./a.out i                   #輸出結果為整數類型

改用C++ 11進行編譯:

$ clang++ main.cpp -std=c++11$ ./a.out i

2 .但是如果還是將auto作為儲存類型修飾符使用,則在C++ 11標準下會出現警告:

#include <iostream>int main(){        int a = 21;        auto int b = a;        //typeid可以擷取變數或者資料類型的名字        std::cout << typeid(b).name() << std::endl;        return 0;}

使用C++ 98編譯:

$ clang++ main.cpp -std=c++98$ ./a.out i

改用C++ 11編譯,出現警告,不再允許作為儲存類型修飾符使用:

$ clang++ main.cpp -std=c++11main.cpp:6:2: warning: ‘auto‘ storage class specifier is not permitted in C++11,      and will not be supported in future releases [-Wauto-storage-class]        auto int b;        ^~~~~1 warning generated.

3 .必須要對auto類型的變數進行初始化,C++ 98中不能單獨使用auto定義變數。

$ clang++ main.cpp -std=c++98main.cpp:6:2: warning: ‘auto‘ type specifier is a C++11 extension      [-Wc++11-extensions]        auto b;        ^main.cpp:6:7: error: declaration of variable ‘b‘ with type ‘auto‘ requires an      initializer        auto b;             ^1 warning and 1 error generated.$ clang++ main.cpp main.cpp:6:2: warning: ‘auto‘ type specifier is a C++11 extension      [-Wc++11-extensions]        auto b;        ^main.cpp:6:7: error: declaration of variable ‘b‘ with type ‘auto‘ requires an      initializer        auto b;             ^1 warning and 1 error generated.
三、擴充

在C++中還能使用decltype來擷取變數或者運算式的類型並用來定義新的變數。

#include <iostream>int main(){        int a = 21;        decltype(a) b;        std::cout << typeid(b).name() << std::endl;        return 0;}

編譯運行結果:

$ clang++ main.cpp -std=c++98$ ./a.out i$ clang++ main.cpp -std=c++11$ ./a.out i

需要注意的是該標準只是改變了C++中auto的用法,但是並沒有影響auto在C語言中的使用!

本文檔由長沙戴維營教育整理。

C++教程之auto關鍵字的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.