【C++】auto关键字推导规则

int x = 0;   
auto *a = &x;  //a->int*,auto被推导为int   
auto b = &x;  //b->int*,auto被推导为int*   
auto &c = x;  //c->int&,auto被推导为int   
const auto d = x;  //d->const int,auto被推导为int      
auto e = c;  //e->int,auto被推导为int,丢弃引用   
auto f = d;  //f->int,auto被推导为int,丢弃const      
const auto& g = x;  //g->const int&,auto被推导为int   
auto& h = g;  //h->const int&,auto被推导为const int,保留const   
auto i = g;  //i->int,auto被推导为int,丢弃引用、丢弃const     

auto的推导规则:
(1)当不声明为指针或是引用时,auto的推导结果和初始化表达式抛弃引用和const属性限定符后的类型一致,图上面的e和f
(2)当声明为指针或是引用时,auto的推导结果将保持初始化表达式的const属性

你可能感兴趣的:(linux,c++)