C++ – does `const auto` have any meaning

cc++11constants

I think the question is clear enough. Will the auto keyword auto-detect const-ness, or always return a non-const type, even if there are eg. two versions of a function (one that returns const and the other that doesn't).

Just for the record, I do use const auto end = some_container.end() before my for-loops, but I don't know if this is necessary or even different from normal auto.

Best Answer

const auto x = expr;

differs from

auto x = expr;

as

const X x = expr;

differs from

X x = expr;

So use const auto and const auto& a lot, just like you would if you didn't have auto.

Overload resolution is not affected by return type: const or no const on the lvalue x does not affect what functions are called in expr.