Search code examples
c++iteratorconstantsoverloadingconst-iterator

How does the Compiler Know Whether to Call the const Overload?


Given string foo, when I call:

auto bar = foo.begin();

There are 2 overloads of string::begin. One returns an string::iterator and the other returns a string::const_iterator. How can I know the type of bar? Is this just based on whether foo is const or not?


Solution

  • Is this just based on whether foo is const or not?

    Yes

    auto is meant for deducing the exact type*


    How to select iterator type using auto variable? indirectly agrees with this answer.

    *Taken from: How do I get a const_iterator using auto?