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

C++C++11Language LawyerConstantsAuto

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsteffenView Question on Stackoverflow
Solution 1 - C++AJG85View Answer on Stackoverflow
Solution 2 - C++AndiDogView Answer on Stackoverflow