Accepting the temporary access address when accessing the address of the element in the <bool> vector

I get a warning only when accessing the address of an element in a bool vector. For a vector of other data types, such as int i, no warnings are received.

eg

vector<bool> boolVect; boolVect.push_back(false); if (boolVect.size() > 0) { cout << &boolVect[0] << endl; } 

I get a warning "accepting the temporary address" in the instruction "cout <& boolVect [0] <end_;
Can someone clarify?

+6
source share
2 answers

std::vector<bool> (see, for example, http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98 or Alternative vector <bool>; ). This is a specialization of std::vector<T> , but the individual elements are stored as packed bits. Therefore, you cannot take the address of an individual element. Therefore, it is really annoying.

+13
source

A vector<bool> is a sample specialization of the vector standard. In a typical implementation, this saves space that each bool only takes up one bit. For convenience, you get a temporary object as a reference for your only bit, which you otherwise could not address.

+3
source

Source: https://habr.com/ru/post/902741/


All Articles