This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: [G++] compilation error: difference between int and bool


On Jun  2, 2001, Florian Weimer <Florian.Weimer@RUS.Uni-Stuttgart.DE> wrote:

> noon@cote-dazur.com (Fabrice Bauzac) writes:

>> class T { vector <bool> v; bool &element (int i) { return v[i]; } };

>> doesn't compile?

> I'm not a C++ guru, but I think you cannot return a non-const
> reference to a temporary object (and a temporary object is required
> because bool and int are different types).

The problem stems from the fact that vector<bool> is a specialization
of the generic vector template class that arranges for each bool to
take only one bit.  Because of this, operator[] cannot return a bool,
since there are no pointers or references to bits, so it returns a
proxy that can be assigned a new bool value or be implicitly converted
to a bool rvalue, which is the case.  As Florian notes, because this
bool is an rvalue, you can't bind a non-const reference to it.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]