Title
Bit-fields in integral promotions
Status
drwp
Section
7.3.7 [conv.prom]
Submitter
Richard Smith

Created on 2021-04-01.00:00:00 last changed 11 months ago

Messages

Date: 2023-02-08.03:23:14

Proposed resolution (approved by CWG 2023-02-07):

  1. Insert a paragraph before 7.3.7 [conv.prom] paragraph 1 as follows:

    For the purposes of 7.3.7 [conv.prom], a converted bit-field is a prvalue that is the result of an lvalue-to-rvalue conversion (7.3.2 [conv.lval]) applied to a bit-field (11.4.10 [class.bit]).
  2. Change in 7.3.7 [conv.prom] paragraph 1 as follows:

    A prvalue of that is not a converted bit-field and has an integer type other than bool, char8_t, char16_t, char32_t, or wchar_t whose integer conversion rank (6.8.6 [conv.rank]) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.
  3. Change in 7.3.7 [conv.prom] paragraph 4 as follows:

    A prvalue of an unscoped enumeration type whose underlying type is fixed (9.7.1 [dcl.enum]) can be converted to a prvalue of its underlying type. Moreover, if integral promotion can be applied to its underlying type, a prvalue of an unscoped enumeration type whose underlying type is fixed can also be converted to a prvalue of the promoted underlying type. [ Note: A converted bit-field of enumeration type is treated as any other value of that type for promotion purposes. -- end note ]
  4. Change in 7.3.7 [conv.prom] paragraph 5 as follows:

    A prvalue for an integral bit-field (11.4.10 [class.bit]) converted bit-field of integral type can be converted to a prvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has enumeration type, it is treated as any other value of that type for promotion purposes.
  5. Move 7.3.7 [conv.prom] paragraph 2 after paragraph 5 and change as follows:

    A prvalue of type char8_t, char16_t, char32_t, or wchar_t (6.8.2 [basic.fundamental]) (including a converted bit-field that was not already promoted to int or unsigned int according to the rules above) can be converted to a prvalue of the first of the following types that can represent all the values of its underlying type: int, unsigned int, long int, unsigned long int, long long int, or unsigned long long int. If none of the types in that list can represent all the values of its underlying type, a prvalue of type char8_t, char16_t, char32_t, or wchar_t can be converted to a prvalue of , or its underlying type.
Date: 2023-06-15.00:00:00

[Accepted as a DR at the June, 2023 meeting.]

According to 7.3.7 [conv.prom] paragraph 5,

A prvalue for an integral bit-field (11.4.10 [class.bit]) can be converted to a prvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.

This description has several problems. First, the “bit-field” semantic property only makes sense for glvalue expressions, so it's unclear why these rules are described as applying to a prvalue. Perhaps this should be rephrased as something like “An expression that was a bit-field glvalue prior to the application of the lvalue-to-rvalue conversion”?

Second, suppose that char32_t is wider than int. Per paragraph 2, a char32_t prvalue promotes to unsigned long (because unsigned long is necessarily at least 32 bits wide). But per paragraph 5, a char32_t : 32 bitfield does not promote. This seems inconsistent.

Finally, it is not clear that the usual integral promotions are not applied to bit-fields. This should be made explicit.

History
Date User Action Args
2023-12-19 10:15:28adminsetstatus: dr -> drwp
2023-07-16 13:00:43adminsetstatus: ready -> dr
2023-06-13 16:05:19adminsetstatus: tentatively ready -> ready
2023-02-08 03:23:14adminsetmessages: + msg7172
2023-02-08 03:23:14adminsetstatus: drafting -> tentatively ready
2022-11-11 09:38:50adminsetstatus: open -> drafting
2021-04-01 00:00:00admincreate