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 

 

posted on 2021-08-09 23:20  python我的最爱  阅读(137)  评论(0编辑  收藏  举报