Title
Variables of floating-point type as switch conditions
Status
c++23
Section
8.5.3 [stmt.switch]
Submitter
Jim X

Created on 2022-09-07.00:00:00 last changed 9 months ago

Messages

Date: 2022-11-10.14:48:13

Proposed resolution (approved by CWG 2022-11-09):

  1. Change in 8.1 [stmt.pre] paragraph 5 as follows:

    ... The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise. ...
  2. Change in 8.5.3 [stmt.switch] paragraph 2 as follows:

    The value of a condition that is an initialized declaration is the value of the declared variable, or the value of the expression otherwise. The value of the condition shall be of integral type, enumeration type, or class type. If of class type, the condition is contextually implicitly converted (7.3 [conv]) to an integral or enumeration type. If the (possibly converted) type is subject to integral promotions (7.3.7 [conv.prom]), the condition is converted to the promoted type. ...
Date: 2022-11-10.14:48:13

CWG telecon 2022-10-21:

Rewording is needed.

Date: 2022-11-15.00:00:00

[Accepted as a DR at the November, 2022 meeting.]

Consider:

switch(float v = 0) {
  case 0: ;
}

Subclause 8.1 [stmt.pre] paragraph 5 specifies:

... The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise. ...

That appears to permit variables of floating-point type, whose value can be converted to integral type. In contrast, expressions of floating-point type are prohibited by 8.5.3 [stmt.switch] paragraph 2:

The condition shall be of integral type, enumeration type, or class type. ...

Possible resolution [SUPERSEDED]:

Change in 8.1 [stmt.pre] paragraph 5 as follows:

... The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type if it has class type; otherwise the program is ill-formed. ...
History
Date User Action Args
2023-07-16 13:00:43adminsetstatus: open -> c++23
2023-07-16 13:00:43adminsetstatus: drwp -> open
2023-02-18 18:43:04adminsetstatus: dr -> drwp
2022-11-25 05:14:04adminsetstatus: ready -> dr
2022-11-10 14:48:13adminsetstatus: review -> ready
2022-11-10 14:48:13adminsetmessages: + msg7014
2022-11-10 14:48:13adminsetstatus: review -> review
2022-11-10 14:48:13adminsetstatus: review -> review
2022-11-10 14:48:13adminsetmessages: + msg6994
2022-11-10 14:48:13adminsetstatus: review -> review
2022-10-30 07:00:36adminsetstatus: open -> review
2022-10-30 07:00:36adminsetstatus: open -> open
2022-10-30 07:00:36adminsetstatus: open -> open
2022-10-30 07:00:36adminsetstatus: open -> open
2022-10-30 06:54:54adminsetstatus: ready -> open
2022-10-30 06:54:54adminsetstatus: ready -> ready
2022-10-30 06:54:54adminsetstatus: ready -> ready
2022-09-07 00:00:00admincreate