reference to vector element

R

Ralf Goertz

Hi,

why doesn't this compile?

#include <vector>

int main() {
std::vector<bool> v(10,true);
bool& br=v[3];
}

g++ complains:

error: invalid initialization of non-const reference of type ‘bool&’
from a temporary of type ‘std::_Bit_reference’

But there are two overloads of [] for std::vector, one reference and one
const reference. So why doesn't the compiler pick the nonconst
reference? Also, neither using at nor dereferencing begin() works.
 
A

Alf P. Steinbach

* Ralf Goertz:
Hi,

why doesn't this compile?

#include <vector>

int main() {
std::vector<bool> v(10,true);
bool& br=v[3];
}

g++ complains:

error: invalid initialization of non-const reference of type ‘bool&’
from a temporary of type ‘std::_Bit_reference’

But there are two overloads of [] for std::vector, one reference and one
const reference. So why doesn't the compiler pick the nonconst
reference? Also, neither using at nor dereferencing begin() works.

std::vector<bool> is special cased to allow an implementation representing each
value as a single bit.

This means you can't refer directly to the bool values inside the vector; it's
unfortunate, a Bad Decision, but we just have to live with it.

You can use the iterators etc. to logically refer to vector elements.


Cheers & hth.,

- Alf
 
G

gwowen

But there are two overloads of [] for std::vector, one reference and one
const reference. So why doesn't the compiler pick the nonconst
reference? Also, neither using at nor dereferencing begin() works.

Unfortunately, due to a horrible oversight during standardization,
std::vector<bool> does not behave like every other std::vector<>. The
standard says that the individual bools can be packed together when
stored in a vector, which is efficient. Sadly, this breaks the
ability to create a reference (const, or otherwise) to an individual
element.

See: http://www.gotw.ca/publications/N1185.pdf for Herb Sutter's
breakdown of the problem.
 
R

Ralf Goertz

gwowen said:
But there are two overloads of [] for std::vector, one reference and one
const reference. So why doesn't the compiler pick the nonconst
reference? Also, neither using at nor dereferencing begin() works.

Unfortunately, due to a horrible oversight during standardization,
std::vector<bool> does not behave like every other std::vector<>. The
standard says that the individual bools can be packed together when
stored in a vector, which is efficient. Sadly, this breaks the
ability to create a reference (const, or otherwise) to an individual
element.

See: http://www.gotw.ca/publications/N1185.pdf for Herb Sutter's
breakdown of the problem.

Thanks. Will that problem be fixed in the new standard?
 
J

Jonathan Lee

error: invalid initialization of non-const reference of type ‘bool&’
from a temporary of type ‘std::_Bit_reference’

You may not be able to use bool& but there is still the type
vector<bool>::reference. It's a level of indirection, but it works
well enough.

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top