auto

p14

auto是一个旧关键字,和static一样是用来声明某局部的(local)。但从未真正使用过,应为不指明static,就隐含为auto

c11的auto加入了根据初始化的值的类型来推断变量类型的功能

auto i = 42;    //i hsa type int
double f();
auto d = f();    //d has type double

以auto声明的变量,其类型根据初值被自动推倒出来,因此一定需要一个初始化操作

auto i;    //error

如果类型很长或者表达式很复杂,auto特别有用:

比如容器迭代器:

vector<string> v;
auto pos = v.begin();    //vector<string>::iterator

lambda表达式返回值:

auto l = [](int x) -> bool {};    //l has type of lambda ,return bool

其他更详细内容参考:http://blog.csdn.net/huang_xw/article/details/8760403

原文地址:https://www.cnblogs.com/raichen/p/5617994.html