位域"In-class initialization"结果为"error: lvalue required as left operand of assignment"
Bit-fields "In-class initialization" results in "error: lvalue required as left operand of assignment"
struct bitfield {
int i = 0; // ok
int j : 8 = 0; // error: lvalue required as left operand of assignment
};
使用c++ 11"类内初始化"功能初始化位字段的正确语法是什么?
这被作为c++标准的核心问题1341提出,但在2015年10月被c++核心工作组拒绝为NAD ("not a defect") -参见http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1341
使用c++ 11"类内初始化"功能初始化位字段的正确语法是什么?
不能在类中初始化位域。c++ 11标准第9.2段规定了类成员声明符的语法:
如你所见,位域成员的声明符不能以大括号或等号初始化式结束。[…]
member-declarator:
声明符virt-specifier-seq(opt) pure-specifier(opt)
说明符brace-or-equal-initializer(选择)
identifier(opt) attribute-specifier-seq(opt): constant-expression
你可以写一个带初始化列表的构造函数,为你的位域提供默认值。
struct bitfield {
int i;
int j : 8;
bitfield() : i(0), j(0) {};
};
您也可以使用默认值创建只读字段。
struct _UserRegister1
{
uint8_t _RES0 : 1;
const uint8_t reserved1 : 1;
uint8_t _HTRE : 1;
const uint8_t reserved2 : 3;
uint8_t _VDDS : 1;
uint8_t _RES1 : 1;
_UserRegister1() : reserved1(1), reserved2(7) {};
};
c++ 11没有提供任何默认初始化位域的语法,但c++ 20提供了。
这意味着当你的c++编译器支持c++ 20时,你的例子编译得很好:
struct bitfield {
int i = 0; // ok
int j : 8 = 0; // ok since C++20
int k : 8 {0}; // ditto
};
请注意,您可能需要显式启用对c++ 20的支持,例如在使用不太新的GCC/Clang版本时使用-std=c++20
。
不能(在c++ 11中)在类中初始化位域。
在MSVC和gcc(带扩展)中,匿名的union
和struct
代码可以让您绕过这一点。
struct bitfield {
int i = 0; // ok
union {
uint32_t raw = 0;
struct {
int j : 8;
int x : 3;
};
};
};
在中,我们将固定大小的raw
与union
混合在位域中,然后在类中初始化raw
元素。
- 为什么我会" void value not ignored as it ought to be"?
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 如何理解"Temporary objs are destroyed as the last step in evaluating the full-expression"?谁能用一些简单的例子来说明这
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误
- r-在Rcpp和C++之间转换矢量(使用Rcpp::as或Rcpp:::wrap)是否会创建一个新的矢量并复制元素
- template<class T, int N> void h(T const(&)[N]); as friend function
- 我正在"void value not ignored as it ought to be"我该怎么办?
- "expression cannot be used as a function"回报声明
- C++指针算术"lvalue required as left operand of assignment"错误
- 重载运算符 [] 并且没有收到"lvalue required as left operand of assignment"错误
- "lvalue required as increment operand" 关于 C++ 和 stl vector's begin()
- 将链接哈希表初始化为 NULL。获取"lvalue required as left operand of assignment"错误。为什么?这是我的代码:
- 列表迭代器,"lvalue required as left operand of assignment"
- "lvalue required as left operand of assignment"在奇怪的地方--C++
- 编写链表时"lvalue required as left operand of assignment"错误
- 位域"In-class initialization"结果为"error: lvalue required as left operand of assignment"
- 错误:"lvalue required as left operand of assignment"
- C++错误"lvalue required as left operand of assignment"