最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++11新特性之auto的使用

 更新時間:2016年12月12日 10:36:11   作者:huang_xw  
熟悉腳本語言的人都知道,很多腳本語言都引入了“類型自動推斷”技術:比如Python,可以直接聲明變量,在運行時進行類型檢查。隨著C++11標準的發(fā)布,C++語言也引入了類型自動推斷的功能。這篇文章主要介紹了C++11新特性之auto的使用,有需要的朋友們可以參考借鑒。

前言

C++是一種強類型語言,聲明變量時必須明確指出其類型。但是,在實踐中,優(yōu)勢我們很難推斷出某個表達式的值的類型,尤其是隨著模板類型的出現(xiàn),要想弄明白某些復雜表達式的返回類型就變得更加困難。為了解決這個問題,C++11中引入的auto主要有兩種用途:自動類型推斷和返回值占位。auto在C++98中的標識臨時變量的語義,由于使用極少且多余,在C++11中已被刪除。前后兩個標準的auto,完全是兩個概念。

一、自動類型推斷

auto自動類型推斷,用于從初始化表達式中推斷出變量的數(shù)據(jù)類型。通過auto的自動類型推斷,可以大大簡化我們的編程工作。下面是一些使用auto的例子。

#include <vector> 
#include <map> 
 
using namespace std; 
 
int main(int argc, char *argv[], char *env[]) 
{ 
// auto a;  // 錯誤,沒有初始化表達式,無法推斷出a的類型 
// auto int a = 10; // 錯誤,auto臨時變量的語義在C++11中已不存在, 這是舊標準的用法。 
 
 // 1. 自動幫助推導類型 
 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ù), 
// 不使用auto將很難確定變量的類型(使用auto后,將由編譯器自動進行確定)。 
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 

三、使用注意事項

1、我們可以使用valatilepointer(*) ,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的表達式必須被初始化

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是一個占位符,并不是一個他自己的類型,因此不能用于類型轉換或其他一些操作,如sizeof和typeid

int value = 123; 
auto x2 = (auto)value; // no casting using auto 
 
auto x3 = static_cast<auto>(value); // same as above 

7、定義在一個auto序列的變量必須始終推導成同一類型

auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this 

8、auto不能自動推導成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會退化成指向數(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] 

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者使用C++能有一定的幫助,如果有疑問大家可以留言交流。

相關文章

最新評論

鄱阳县| 长春市| 巴塘县| 宜阳县| 邵阳市| 东辽县| 原阳县| 扶风县| 汝州市| 西畴县| 焦作市| 江永县| 乡宁县| 安远县| 沾化县| 盈江县| 兰坪| 任丘市| 东辽县| 天台县| 临江市| 如东县| 朝阳市| 黄骅市| 汉阴县| 贵阳市| 兴文县| 兰考县| 丹东市| 安康市| 桂林市| 九江市| 新田县| 金堂县| 凤凰县| 江油市| 临泉县| 登封市| 托里县| 公安县| 连云港市|