-2

Possible Duplicate:
taking address of temporary while accessing address of a element in vector<bool>

I get 'taking address of temporary' warning at line 7 in following piece of code

vector<bool> boolVect;
bool x = true;
boolVect.push_back(true);
boolVect.push_back(false);
ofstream fMetaOut("tmp.txt", ios::out);
fMetaOut.write((char* )&x, sizeof(bool));
fMetaOut.write((char* )&boolVect[0], sizeof(bool));
fMetaOut.close();

Why do I get this warning at line 7 and not at 6 ?? Both use boolean address.
How can I get rid of this warning?

gmargari
  • 171
  • 1
  • 9
Rahul
  • 257
  • 1
  • 4
  • 11
  • 9
    Didn't you have this same issue [five months ago](http://stackoverflow.com/questions/8324699/taking-address-of-temporary-while-accessing-address-of-a-element-in-vectorbool)? – Blastfurnace May 10 '12 at 05:16
  • 1
    @Blastfurnace there's no way you remembered that... is there? Tell me it's a coincidence you came across his old question! – Mahmoud Al-Qudsi May 10 '12 at 05:27
  • @MahmoudAl-Qudsi: Well, simply click on Rahul's user name, you'll see all his previous questions :) – Gui13 May 10 '12 at 05:32
  • @MahmoudAl-Qudsi: I have a habit of looking at people's profiles and the third question down had the same warning message in the title. – Blastfurnace May 10 '12 at 05:32

2 Answers2

6

x is not an temporary, it is a l-value, an automatic variable which resides on stack.
While, &boolVect[0] is not an l-value.
l-value is an entity whose address can be taken, in short a variable whose lifetime is long enough to reside in the memory so that it can be addressed by an name.

What is problem with vector<bool>?? It works fine for vector<int>

The C++ standard library provides a specialization of vector for boolean types. The goal is optimization, to use less size than a usual implementation of vector for type bool. A usual implementation for bool would reserve at least 1 byte for each element. The vector<bool> specialization usually uses internally only 1 bit for an element, so it is typically eight times smaller. But this optimization comes with a price:
In C++, the smallest addressable value must have a size of at least 1 byte. Thus, such a specialization of a vector needs special handling for references and iterators. As a result, a vector<bool> does not meet all requirements of other vectors. The behavior you see is one such example, elements of the vector<bool> are not a true l-values.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

Unlike other specialisations of vector, vector<bool> does not manage an array of bool objects. Instead, it manages a packed bitfield.

Since the bits are not individually addressable, boolVect[0] is not a reference to a bool; it's a (temporary) object of a type that's convertible to bool. There is no way to get a reference or pointer to a vector element.

If you want a container of boolean flags that you can take references to, then vector<char> or deque<bool> might be suitable choices.

Taking the address of x is fine, since it is a (non-temporary) bool object.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644