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

Created on 2022-09-07.00:00:00 last changed 3 weeks ago

Messages

Date: 2022-09-07.00:00:00

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:

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
2022-09-07 00:00:00admincreate