That two for loop is the same
The 1st loop, I manually use const_iterator but I didnt use any constant reference to my vector? So how does the compiler deduce to this type?
And if you will read article, please not that it is outdated: in C++11 standard library was fixed and allows usage of const_iterators in all sensible places. In Effective Modern C++ Scott Meyers now advises to use const_iterators if possible and discuss changes made in C++11 (Item 13)
OW no no. I merely put it to show that the compiler is okay with const_iterator rather that iterator only. Putting auto makes it const_iterator. It was just for illustration. My concern is how and why does compiler go to const_iterator even if my vector isnt constant
clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors -Wno-unused-parameter main.cpp -lsupc++ && ./a.out
ca: const A
ca.c: const int
ca.i: const int
ca.m: (non-const) int
ca.foo() => A::foo() const => this: pointer to const A
a: (non-const) A
a.c: const int
a.i: (non-const) int
a.m: (non-const) int
a.foo() => A::foo() const => this: pointer to const A
a.bar() => A::bar() (non-const) => this: pointer to (non-const) A