【c++】auto的使用

来源:互联网 发布:网络简介阅读题答案 编辑:程序博客网 时间:2024/05/20 07:59

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

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

1. 自动类型推断
auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过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. 使用模板技术时,如果某个变量的类型依赖于模板参数,  // 不使用auto将很难确定变量的类型(使用auto后,将由编译器自动进行确定)。  template <class T, class U>  void Multiply(T t, U u)  {      auto v = t * u;  }  

2. 返回值占位

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  

3.使用注意事项

①我们可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto

auto k = 5;  auto* pK = new auto(k);  auto** ppK = new auto(&k);  const auto n = 6;  

②用auto声明的变量必须初始化

auto m; // m should be intialized   

③auto不能与其他类型组合连用

auto int p; // 这是旧auto的做法。 

④函数和模板参数不能被声明为auto

void MyFunction(auto parameter){} // no auto as method argument  template<auto T> // utter nonsense - not allowed  void Fun(T t){}  

⑤定义在堆上的变量,使用了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)

⑥以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid

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

⑦定义在一个auto序列的变量必须始终推导成同一类型

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

⑧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

⑨auto会退化成指向数组的指针,除非被声明为引用

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] 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机做话筒连蓝牙音箱不同步怎么办 荣耀畅玩7x触屏不灵怎么办 华为6x屏幕点没反应怎么办 苹果耳机按一下就挂断电话怎么办 华为畅享7忘记账号密码怎么办 华为荣耀8手机外放声音小怎么办 安卓手机耳机减音量不能用怎么办 苹果手机微信语音是耳机模式怎么办 平果手机没声音出现耳机模式怎么办 苹果6s进水了一直耳机模式怎么办 华为手机没声音显示耳机模式怎么办 华为手机没声音出现耳机模式怎么办 安卓手机全民k歌耳返延迟怎么办 华为手机双清后手机开不了机怎么办 荣耀9上面有个耳机标志怎么办 华为p9耳机有一个没有声音怎么办 小米5c手机gps信号弱怎么办 华为8手机时常听常音乐声怎么办 手机用久了变慢了怎么办 免税店买的皮带太短了怎么办 小米手机自拍照片是反的怎么办 华为p20旅行助手被删了怎么办 假如手机点击一个链接是病毒怎么办 华为手机自带铃声没了怎么办 华为手机升级后铃声没了怎么办 华为平板电脑激活锁忘记了怎么办 华为手机不小心删了系统应用怎么办 华为畅想6s开机键坏了怎么办 浏览网页是进入有病毒的网页怎么办 华为荣耀7清理加速那么慢怎么办 华为手机自带天气卸载了怎么办 手机被病毒感染了开不了机了怎么办 小米手机系统桌面已停止运行怎么办 三星手机应用锁密码忘了怎么办 手机管家应用加密密码忘记了怎么办 下载东西呗安全管家制止了该怎么办 手机百度时不小心中毒扣话费怎么办 手机扫二维码中了木马病毒要怎么办 电脑管家微信扫描语音打不开怎么办 遇到花心老公又爱玩没有担当怎么办 软件全闪退返回键不管用了怎么办