【C++11】新特性——auto的使用

来源:互联网 发布:淘宝首页全屏装修教程 编辑:程序博客网 时间:2024/05/19 11:38

转自:http://blog.csdn.net/huang_xw/article/details/8760403

C++11中引入的auto主要有两种用途:自动类型推断和返回值占位。auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念。

1. 自动类型推断

    auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推断,可以大大简化我们的编程工作。下面是一些使用auto的例子。
[cpp] view plaincopyprint?
  1. #include <vector>  
  2. #include <map>  
  3.   
  4. using namespace std;  
  5.   
  6. int main(int argc, char *argv[], char *env[])  
  7. {  
  8. //  auto a;                 // 错误,没有初始化表达式,无法推断出a的类型  
  9. //  auto int a = 10;        // 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。  
  10.   
  11.     // 1. 自动帮助推导类型  
  12.     auto a = 10;  
  13.     auto c = 'A';  
  14.     auto s("hello");  
  15.   
  16.     // 2. 类型冗长  
  17.     map<int, map<int,int> > map_;  
  18.     map<int, map<int,int>>::const_iterator itr1 = map_.begin();  
  19.     const auto itr2 = map_.begin();  
  20.     auto ptr = []()  
  21.     {  
  22.         std::cout << "hello world" << std::endl;  
  23.     };  
  24.   
  25.     return 0;  
  26. };  
  27.   
  28. // 3. 使用模板技术时,如果某个变量的类型依赖于模板参数,  
  29. // 不使用auto将很难确定变量的类型(使用auto后,将由编译器自动进行确定)。  
  30. template <class T, class U>  
  31. void Multiply(T t, U u)  
  32. {  
  33.     auto v = t * u;  
  34. }  

2. 返回值占位

[cpp] view plaincopyprint?
  1. template <typename T1, typename T2>  
  2. auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)  
  3. {  
  4.    return t1+t2;  
  5. }  
  6. auto v = compose(2, 3.14); // v's type is double  

3.使用注意事项

①我们可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto
[cpp] view plaincopyprint?
  1. auto k = 5;  
  2. auto* pK = new auto(k);  
  3. auto** ppK = new auto(&k);  
  4. const auto n = 6;  
②用auto声明的变量必须初始化
[cpp] view plaincopyprint?
  1. auto m; // m should be intialized    
③auto不能与其他类型组合连用
[cpp] view plaincopyprint?
  1. auto int p; // 这是旧auto的做法。  
④函数和模板参数不能被声明为auto
[cpp] view plaincopyprint?
  1. void MyFunction(auto parameter){} // no auto as method argument  
  2.   
  3. template<auto T> // utter nonsense - not allowed  
  4. void Fun(T t){}  
⑤定义在堆上的变量,使用了auto的表达式必须被初始化
[cpp] view plaincopyprint?
  1. int* p = new auto(0); //fine  
  2. int* pp = new auto(); // should be initialized  
  3.    
  4. auto x = new auto(); // Hmmm ... no intializer  
  5.      
  6. auto* y = new auto(9); // Fine. Here y is a int*  
  7. auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)  
⑥以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
[cpp] view plaincopyprint?
  1. int value = 123;  
  2. auto x2 = (auto)value; // no casting using auto  
  3.   
  4. auto x3 = static_cast<auto>(value); // same as above   
⑦定义在一个auto序列的变量必须始终推导成同一类型
[cpp] view plaincopyprint?
  1. auto x1 = 5, x2 = 5.0, x3='r';  // This is too much....we cannot combine like this  
⑧auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型
[cpp] view plaincopyprint?
  1. const int i = 99;  
  2. auto j = i;       // j is int, rather than const int  
  3. j = 100           // Fine. As j is not constant  
  4.   
  5. // Now let us try to have reference  
  6. auto& k = i;      // Now k is const int&  
  7. k = 100;          // Error. k is constant  
  8.   
  9. // Similarly with volatile qualifer  
⑨auto会退化成指向数组的指针,除非被声明为引用
[cpp] view plaincopyprint?
  1. int a[9];  
  2. auto j = a;  
  3. cout<<typeid(j).name()<<endl; // This will print int*  
  4.   
  5. auto& k = a;  
  6. cout<<typeid(k).name()<<endl; // This will print int [9]  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 阴茎勃起后向上翘的厉害怎么办 5个月宝宝发烧38.5度怎么办 9个月宝宝发烧38.5度怎么办 八个月宝宝只吃母乳不吃奶粉怎么办 八个月母乳不够宝宝不吃奶粉怎么办 八个月宝宝吃母乳不吃奶粉怎么办 八个月宝宝戒奶不吃奶粉怎么办 刚满月的宝宝发烧38度怎么办 未满月的宝宝发烧38度怎么办 半月大的婴儿吃奶就漾奶怎么办 上司交给你不能完成的任务怎么办 电脑光驱里放入光碟放不出来怎么办 黑暗之魂3太难了怎么办 苹果手机下载的游戏闪退怎么办 宝宝两岁了不怎么爱拉大便怎么办? 小狗脖子发硬疼的直叫怎么办 厨房里有很多小虫子围着鸡蛋怎么办 狗生小狗后几天不吃饭怎么办 还没满月的小兔子突然死了怎么办 宝宝小鸡被蚊子咬后肿得很大怎么办 不知道是哪知兔子下的小兔怎么办 兔子生完小兔不吃东西了怎么办 人工喂养七天的小羊拉希怎么办 仔兔出生3天吃过奶就尿怎么办 小兔子买回来两天不拉屎怎么办 大狗生了小狗把小狗咬死了怎么办 狗妈妈一直咬小狗的脐带怎么办 狗狗体内驱虫驱不干净怎么办 打老鼠脚被老鼠咬了怎么办 天正画的cad打开显示空白怎么办 苹果手机信息被拉进群聊怎么办 空调的控制线的报验资料怎么办 窗窗户罩子护栏上的瓦楞板怎么办 酸洗好的带钢容易返锈怎么办 化肥撤到小树苗上现在变黑了怎么办 尿素液烧的太慢了怎么办 天堂鸟肥料施多了黄叶了怎么办 死水塘养的鱼每天在死怎么办 北京的阿姆斯肥把苗都烧死了怎么办 纱窗被老鼠咬了个洞 怎么办 低电量模式下动态墙纸不能用怎么办