Nmd*_*ery 0 c++ iterator lvalue
我得到一个C2440('初始化':无法从'std :: _ Vb_reference <_Alloc>'转换为'bool&'),IntelliSense转换为标题中的错误.
我得到了这个错误所说的内容,而不是为什么会这么说.下面的代码产生此错误:
std::vector<const UINT>::iterator oIter;
oIter = std::find(vecuClassID.begin(), vecuClassID.end(), uClassID);
const UINT uDistance = std::distance(vecuClassID.begin(), oIter);
bool& refbStaticSectionInitialized = *(vecbStaticSectionInitialized.begin() + uDistance);
Run Code Online (Sandbox Code Playgroud)
错误似乎发生在最后一行 - 在Visual Studio中,取消引用运算符以红色下划线.这很令人困惑,因为我的代码与CRITICAL_SECTION完全相同,并且不会产生错误:
std::vector<const UINT>::iterator oIter;
oIter = std::find(vecuClassID.begin(), vecuClassID.end(), uClassID);
const UINT uDistance = std::distance(vecuClassID.begin(), oIter);
CRITICAL_SECTION& refhStaticSection = *(vechStaticSection.begin() + uDistance);
Run Code Online (Sandbox Code Playgroud)
它与bool是一个原始的东西有关吗?
问题是std::vector<bool>不会bool&从其下标运算符返回或者在解除引用其迭代器时返回.相反,返回的类型是不转换std::vector<bool>::reference为的类.bool&
背后的错误思想std::vector<bool>是调整界面以允许打包表示.由于某个位不可寻址,因此std::vector<bool>::reference可作为一个位的代理.