Is 'auto const' and 'const auto' t

2020-02-24 05:15发布

Is there a semantic difference between auto const and const auto, or do they mean the same thing?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-02-24 05:33

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*).

查看更多
Deceive 欺骗
3楼-- · 2020-02-24 05:46

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.

查看更多
登录 后发表回答