Search code examples
c++c++11constants

does `const auto` have any meaning?


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.


Solution

  • Maybe you are confusing const_iterator and const iterator. The first one iterates over const elements, the second one cannot iterate at all because you cannot use operators ++ and -- on it.

    Note that you very seldom iterate from the container.end(). Usually you will use:

    const auto end = container.end();
    for (auto i = container.begin(); i != end; ++i) { ... }