标签:
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属性
标签:
原文地址:http://www.cnblogs.com/slgnesin/p/5295603.html