码迷,mamicode.com
首页 > 其他好文 > 详细

auto type用途

时间:2016-03-19 17:42:51      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

It is not uncommon to want to store the value of an expression in a variable. To
declare the variable, we have to know the type of that expression. When we write a
program, it can be surprisingly difficult—and sometimes even impossible—to
determine the type of an expression. Under the new standard, we can let the compiler
figure out the type for us by using the auto type specifier. Unlike type specifiers, such
as double, that name a specific type, auto tells the compiler to deduce the type
from the initializer.

auto item=val1+val2;

 以下语句是错误的:

auto a=0,b=3.14;//变量a与b类型不一致

auto要求在同一行内定义的变量类型一致

auto通常忽略顶层const,而不忽略底层const,如:

const int ci=i,&cr=ci;
auto b=ci;//顶层const被忽略
auto c=cr;//cr是顶层constci的别名
auto e=&ci;//e是const对象的指针,e是底层const

若希望auto类型具有顶层const属性,则需要显示地声明:

const auto b=ci;//此时b具有顶层const属性

 

auto type用途

标签:

原文地址:http://www.cnblogs.com/slgnesin/p/5295603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!