C++基础-auto(自动分配属性)和decltype(指定分配属性)

1.atuo 自动分配属性,但是不能区分常量, decltype可以区分常量

const vector<int> myint{1, 2, 3, 4, 5};
auto inta = myint[0];
inta = 20;

decltype(myint[0]) intd = 1;
intd = 2; //报错,因为此时的intd是常量属性

2.auto不能区分引用,decltype可以区分引用

double db = 10.9;
double &rdb(db);
auto dbx = rdb; //如果能区别
dbx = 8.9;
cout << db << endl;  //10.9 
cout << dbx << endl; //8.9 

    decltype(rdb) dbx1 = rdb;
    dbx1 = 8.9;
    cout << db << endl; //8.9 
    cout << dbx1 << endl; //8.9 
原文地址:https://www.cnblogs.com/my-love-is-python/p/15121205.html