Files
2025-10-25 03:02:53 +03:00

117 lines
5.0 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[class.bit]
# 11 Classes [[class]](./#class)
## 11.4 Class members [[class.mem]](class.mem#class.bit)
### 11.4.10 Bit-fields [class.bit]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2871)
A [*member-declarator*](class.mem.general#nt:member-declarator "11.4.1General[class.mem.general]") of the form
[*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]")opt [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt : [*constant-expression*](expr.const#nt:constant-expression "7.7Constant expressions[expr.const]") [*brace-or-equal-initializer*](dcl.init.general#nt:brace-or-equal-initializer "9.5.1General[dcl.init.general]")opt
specifies a bit-field[.](#1.sentence-1)
The optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") appertains
to the entity being declared[.](#1.sentence-2)
A bit-field shall not be a static member[.](#1.sentence-3)
A bit-field shall have integral or (possibly cv-qualified) enumeration type;
the bit-field semantic property is not part of the type of the class member[.](#1.sentence-4)
The [*constant-expression*](expr.const#nt:constant-expression "7.7Constant expressions[expr.const]") shall be an integral constant expression
with a value greater than or equal to zero and
is called the [*width*](simd.general#def:width "29.10.1General[simd.general]") of the bit-field[.](#1.sentence-5)
If the width of a bit-field is larger than
the width of the bit-field's type
(or, in case of an enumeration type, of its underlying type),
the extra bits are padding bits ([[basic.types.general]](basic.types.general#term.padding.bits "6.9.1General"))[.](#1.sentence-6)
Allocation of bit-fields within a class object isimplementation-defined[.](#1.sentence-7)
Alignment of bit-fields is implementation-defined[.](#1.sentence-8)
Bit-fields are packed into some addressable allocation unit[.](#1.sentence-9)
[*Note [1](#note-1)*:
Bit-fields straddle allocation units on some machines and not on others[.](#1.sentence-10)
Bit-fields are assigned right-to-left on some machines, left-to-right on
others[.](#1.sentence-11)
— *end note*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2905)
A declaration for a bit-field that omits the [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]") declares an [*unnamed bit-field*](#def:bit-field,unnamed "11.4.10Bit-fields[class.bit]")[.](#2.sentence-1)
Unnamed bit-fields are not
members and cannot be initialized[.](#2.sentence-2)
An unnamed bit-field shall not be declared with a cv-qualified type[.](#2.sentence-3)
[*Note [2](#note-2)*:
An unnamed bit-field is useful for padding to conform to
externally-imposed layouts[.](#2.sentence-4)
— *end note*]
As a special case, an unnamed bit-field with a width of zero specifies
alignment of the next bit-field at an allocation unit boundary[.](#2.sentence-5)
Only
when declaring an unnamed bit-field may the width be zero[.](#2.sentence-6)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2920)
The address-of operator & shall not be applied to a bit-field,
so there are no pointers to bit-fields[.](#3.sentence-1)
A non-const reference shall not bind to a bit-field ([[dcl.init.ref]](dcl.init.ref "9.5.4References"))[.](#3.sentence-2)
[*Note [3](#note-3)*:
If the initializer for a reference of type const T& is
an lvalue that refers to a bit-field, the reference is bound to a
temporary initialized to hold the value of the bit-field; the reference
is not bound to the bit-field directly[.](#3.sentence-3)
See [[dcl.init.ref]](dcl.init.ref "9.5.4References")[.](#3.sentence-4)
— *end note*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L2935)
If a value of integral type (other than bool) is stored
into a bit-field of width N and the value would be representable
in a hypothetical signed or unsigned integer type
with width N and the same signedness as the bit-field's type,
the original value and the value of the bit-field compare equal[.](#4.sentence-1)
If the value true or false is stored into a bit-field of
type bool of any size (including a one bit bit-field), the
original bool value and the value of the bit-field compare
equal[.](#4.sentence-2)
If a value of an enumeration type is stored into a bit-field of the
same type and the width is large
enough to hold all the values of that enumeration type ([[dcl.enum]](dcl.enum "9.8.1Enumeration declarations")),
the original value and the value of the bit-field compare equal[.](#4.sentence-3)
[*Example [1](#example-1)*: enum BOOL { FALSE=0, TRUE=1 };struct A { BOOL b:1;};
A a;void f() { a.b = TRUE; if (a.b == TRUE) // yields true{ /* ... */ }} — *end example*]