1.auto关键字

使用auto可以自动推导出变量类型,如

auto x=5;     //x是int类型

static auto y=0.0    //y是double类型

auto pi=new auto(1)    //pi被推到为int*

 

2.auto的推导规则

再来看一组例子

int x=0;

auto * a=&x;      //a被推导为int

auto   b=&x;      //b被推导为int*

auto  & c=x;      //c被推导为int&

const auto e=x;    //e被推导为int

auto f=e;     //会截断const,f推导为int

 

3.auto的限制

auto不能用于函数参数

auto不能用于非静态成员变量

 

4.decltype关键字

如:

int x=0;

decltype(x) y=1;   //y  ->  int

decltype(x+y) z=0;   //z -> int

 

相关文章: