I am getting the warning only while accessing address of element in vector of bool. For vector of other data types like int i don't get any warning.
eg
vector<bool> boolVect;
boolVect.push_back(false);
if (boolVect.size() > 0) {
cout << &boolVect[0] << endl;
}
I get warning "taking address of temporary" at statement "cout << &boolVect[0] << endl;"
Can someone please clarify?
std::vector<bool>
is broken (see e.g. http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98 or Alternative to vector<bool>). It's a specialization of std::vector<T>
, but the individual elements are stored as packed bits. Therefore, you can't take the address of an individual element. Therefore, it's really annoying.
A vector<bool>
is a template specialization of the standard vector
. In a normal implementation it saves space, that every bool
only takes one bit. For convenience you get a temporary object as a reference for your single bit which you otherwise could not address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With