Is 'auto const' and 'const auto' the same?

北慕城南 提交于 2019-11-30 07:48:14

The const qualifier applies to the type to the immediate left unless there is nothing to the left then it applies to the type to the immediate right. So yup it's the same.

AndiDog

Contrived example:

std::vector<char*> test;
const auto a = test[0];
*a = 'c';
a = 0; // does not compile
auto const b = test[1];
*b = 'c';
b = 0; // does not compile

Both a and b have type char* const. Don't think you can simply "insert" the type instead of the keyword auto (here: const char* a)! The const keyword will apply to the whole type that auto matches (here: char*).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!