首页 > 其他 > 详细

auto

时间:2016-06-26 16:42:10      阅读:180      评论:0      收藏:0      [点我收藏+]

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

auto

原文:http://www.cnblogs.com/raichen/p/5617994.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!