使用bool作为l-value
Using bool as l-value
当类型为int时编译正常,但当Type是bool,则给出"错误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
。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将Ref对象作为类成员
- 虚拟决赛作为安全
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 使用指向成员的指针将成员函数作为参数传递
- 我应该使用什么来代替void作为变体中的替代类型之一
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 何时提供默认参数作为模板参数
- 如何将 I->getType() 作为参数传递给 llvm 中的 CreateCall?
- C++使用数组作为多维数组,尽管将其初始化为带有指针的 1D
- C++匿名结构作为std::映射值
- C++:如何使函数只返回作为列表一部分的字符串
- 如何制作一个将函数作为参数的类方法
- *&在这里做什么?如何更改它以获取代码作为 void binaryTree::insert(binaryNode *root,string value)?可能吗?
- 使用bool作为l-value
- 枚举类型value作为c++中数组的长度
- 获取默认初始化(NOT value/零初始化)POD作为右值
- const或ref或const ref或value作为setter函数的参数
- 我认为将 JsonValue 值分配给 JsonValue 可能非常慢,与将 Value 作为键相比