C++11新特性之a(chǎn)uto的使用
前言
C++是一種強(qiáng)類型語言,聲明變量時(shí)必須明確指出其類型。但是,在實(shí)踐中,優(yōu)勢(shì)我們很難推斷出某個(gè)表達(dá)式的值的類型,尤其是隨著模板類型的出現(xiàn),要想弄明白某些復(fù)雜表達(dá)式的返回類型就變得更加困難。為了解決這個(gè)問題,C++11中引入的auto主要有兩種用途:自動(dòng)類型推斷和返回值占位。auto在C++98中的標(biāo)識(shí)臨時(shí)變量的語義,由于使用極少且多余,在C++11中已被刪除。前后兩個(gè)標(biāo)準(zhǔn)的auto,完全是兩個(gè)概念。
一、自動(dòng)類型推斷
auto自動(dòng)類型推斷,用于從初始化表達(dá)式中推斷出變量的數(shù)據(jù)類型。通過auto的自動(dòng)類型推斷,可以大大簡化我們的編程工作。下面是一些使用auto的例子。
#include <vector>
#include <map>
using namespace std;
int main(int argc, char *argv[], char *env[])
{
// auto a; // 錯(cuò)誤,沒有初始化表達(dá)式,無法推斷出a的類型
// auto int a = 10; // 錯(cuò)誤,auto臨時(shí)變量的語義在C++11中已不存在, 這是舊標(biāo)準(zhǔn)的用法。
// 1. 自動(dòng)幫助推導(dǎo)類型
auto a = 10;
auto c = 'A';
auto s("hello");
// 2. 類型冗長
map<int, map<int,int> > map_;
map<int, map<int,int>>::const_iterator itr1 = map_.begin();
const auto itr2 = map_.begin();
auto ptr = []()
{
std::cout << "hello world" << std::endl;
};
return 0;
};
// 3. 使用模板技術(shù)時(shí),如果某個(gè)變量的類型依賴于模板參數(shù),
// 不使用auto將很難確定變量的類型(使用auto后,將由編譯器自動(dòng)進(jìn)行確定)。
template <class T, class U>
void Multiply(T t, U u)
{
auto v = t * u;
}
二、返回值占位
template <typename T1, typename T2>
auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)
{
return t1+t2;
}
auto v = compose(2, 3.14); // v's type is double
三、使用注意事項(xiàng)
1、我們可以使用valatile,pointer(*) ,reference(&) ,rvalue reference(&&) 來修飾auto
auto k = 5; auto* pK = new auto(k); auto** ppK = new auto(&k); const auto n = 6;
2、用auto聲明的變量必須初始化
auto m; // m should be intialized
3、auto不能與其他類型組合連用
auto int p; // 這是舊auto的做法。
4、函數(shù)和模板參數(shù)不能被聲明為auto
void MyFunction(auto parameter){} // no auto as method argument
template<auto T> // utter nonsense - not allowed
void Fun(T t){}
5、定義在堆上的變量,使用了auto的表達(dá)式必須被初始化
int* p = new auto(0); //fine int* pp = new auto(); // should be initialized auto x = new auto(); // Hmmm ... no intializer auto* y = new auto(9); // Fine. Here y is a int* auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)
6、以為auto是一個(gè)占位符,并不是一個(gè)他自己的類型,因此不能用于類型轉(zhuǎn)換或其他一些操作,如sizeof和typeid
int value = 123; auto x2 = (auto)value; // no casting using auto auto x3 = static_cast<auto>(value); // same as above
7、定義在一個(gè)auto序列的變量必須始終推導(dǎo)成同一類型
auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this
8、auto不能自動(dòng)推導(dǎo)成CV-qualifiers(constant & volatile qualifiers),除非被聲明為引用類型
const int i = 99; auto j = i; // j is int, rather than const int j = 100 // Fine. As j is not constant // Now let us try to have reference auto& k = i; // Now k is const int& k = 100; // Error. k is constant // Similarly with volatile qualifer
9、auto會(huì)退化成指向數(shù)組的指針,除非被聲明為引用
int a[9]; auto j = a; cout<<typeid(j).name()<<endl; // This will print int* auto& k = a; cout<<typeid(k).name()<<endl; // This will print int [9]
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用C++能有一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
C++中std::partial_sort的使用小結(jié)
std::partial_sort?是?C++?標(biāo)準(zhǔn)庫中的一個(gè)算法,它可以對(duì)容器中的一部分元素進(jìn)行排序,本文主要介紹了C++中std::partial_sort的使用小結(jié),感興趣的可以了解一下2025-04-04
用VC++6.0的控制臺(tái)實(shí)現(xiàn)2048小游戲的程序
本文是作者拜讀劉地同學(xué)的《C語言控制臺(tái)版2048》之后感覺非常不錯(cuò),添加了注釋之后分享給大家的,方便更多的初學(xué)者閱讀學(xué)習(xí),有需要的小伙伴參考下。2015-03-03
C語言實(shí)現(xiàn)大數(shù)據(jù)文件的內(nèi)存映射機(jī)制
這篇文章主要介紹了C語言實(shí)現(xiàn)大數(shù)據(jù)文件的內(nèi)存映射機(jī)制的相關(guān)資料,需要的朋友可以參考下2017-01-01
C++實(shí)現(xiàn):螺旋矩陣的實(shí)例代碼
螺旋矩陣是指一個(gè)呈螺旋狀的矩陣,它的數(shù)字由第一行開 始到右邊不斷變大,向下變大, 向左變大,向上變大,如此循環(huán)。2013-03-03
C語言字符函數(shù)與字符串函數(shù)的實(shí)現(xiàn)示例
C語言標(biāo)準(zhǔn)庫中的<ctype.h>和<string.h>頭文件分別提供了豐富的字符處理和字符串處理函數(shù),本文就來介紹一下C語言字符函數(shù)與字符串函數(shù)的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-11-11
淺析C++調(diào)用Java的Jar包(帶參數(shù))問題
這篇文章主要介紹了C++調(diào)用Java的Jar包(帶參數(shù))問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
關(guān)于CLion配置visual?studio(msvc)和JOM多核編譯的問題
這篇文章主要介紹了CLion配置visual?studio(msvc)和JOM多核編譯,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07

