Search code examples
c++c++11auto

variable declared by auto sometimes is by reference?


I just tested this piece of code.

vector<bool> v(5, true);
if(v.back())cout<<"====="<<endl;
auto b1 = v.back();
b1 = false;
cout<<&b1<<endl;
if(v.back())cout<<"*********"<<endl;

My questions are following:

  1. "*********" (no quotes) doesn't appear in the output, why the variable declared by auto is changing the bool vector v?
  2. I understand that vector<bool> is not a standard STL container, and addressing the element of it by &v[4] won't work(since you can not address the address of a bit), if b1 is declared by a reference to v.back(), why I can address b1 by &b1?
  3. In what cases auto has this kind of behavior? Does auto c1 = v.begin() and later doing c1 = (++v.begin()) will change v.begin()?

Solution

  • std::vector<bool> is a failure in the standard library, a vector<T> which is not a container of Ts.

    Thus, it has significantly different behavior from all other instantiations of vector.

    The specific wart you stumbled over is that it's member-type reference is a proxy class representing a reference to a single bool.

    Which means that auto, which never deduces as a reference, deduced as that proxy-class, will behave as if it was a reference.

    &v[4] won't work for getting a pointer to the bool at index 4, because vector<bool> is not a container of bool and the index-operator also returns those proxy-classes.

    Naturally, vector<bool> has really special iterators which allow iterating over the bit-set, so using iterators only has the handicap that dereferencing an iterator also returns a proxy.