使用bool作为l-value

Using bool as l-value

本文关键字:l-value 作为 bool 使用      更新时间:2023-10-16

类型int时编译正常,但当Typebool,则给出"错误C2102:'&'需要l-value"。为什么?

std::vector<Type> Vector;
Vector.push_back(false);
Vector.push_back(true);
Vector.push_back(true);
const void* Pointer = NULL;
for (std::vector<Type>::const_iterator it = Vector.begin(); it != Vector.end(); ++it)
    Pointer = &(*it);

vector专门用于bool,在该专门化中,*it不是左值。不可能指向矢量内的单个bool

事后看来,这种专业化被认为是一个错误。现在通常建议避免使用vector<bool>。备选方案包括std::bitset