No two of the case constants in the same switch shall have the same value after conversion.
[stmt.switch] p3 says:
There shall be at most one label of the form
default :within a switch statement.
Consider the following example:
switch (1)
{
case 1: // the first case 1 label within the outer switch
default: // the first default label within the outer switch
switch (1)
{
case 1: // Error: the second case 1 label within the outer switch
default: // Error: the second default label within the outer switch
}
}
case and default labels associated with the inner switch statement are also within the outer switch statement.
Suggested resolution:
Use “associated with” throughout [stmt.switch], suggested wording changes:
Remove part of [stmt.switch] p2 and the entire [stmt.switch] p3-5:
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 to an integral or enumeration type.
If the (possibly converted) type is subject to integral promotions, the condition is converted to the
promoted type. Any statement within the switch statement can be labeled with one or more case labels as
follows:
case constant-expression :
where the constant-expression shall be a converted constant expression of the adjusted type of the switch
condition. No two of the case constants in the same switch shall have the same value after conversion.
There shall be at most one label of the form
default :
within a switch statement.
Switch statements can be nested; a case or default label is associated with the smallest switch enclosing it.
When the switch statement is executed, its condition is evaluated. If one of the case constants has the same
value as the condition, control is passed to the statement following the matched case label. If no case constant
matches the condition, and if there is a default label, control passes to the statement labeled by the default
label. If no case matches and if there is no default then none of the statements in the switch is executed.
Add the following paragraphs as [stmt.switch] p3-5:
A case or default label is associated with the smallest switch statement enclosing ([stmt.pre]) it. There shall be at most one default label associated with each switch statement.
The constant-expression of each case label shall be a converted constant expression ([expr.const]) of the adjusted type of the condition of the case label's associated switch statement. No two case labels associated with the same switch statement shall have constant-expression of the same value after conversion.
When the switch statement is executed, its condition is evaluated. 1. If the constant-expression of a case label associated with the switch statement has the same value as the condition after conversion, control is passed to the statement labeled by the matched case label. 2. Otherwise, if there is a default label associated with the switch statement, control is passed to the statement labeled by the default label. 3. Otherwise, the statement of the switch statement is not executed.
Full name of submitter: Geng Cheng
Reference: [stmt.switch]
Issue description:
[stmt.switch] p2 says:
[stmt.switch] p3 says:
Consider the following example:
case
anddefault
labels associated with the innerswitch
statement are also within the outerswitch
statement.Suggested resolution: Use “associated with” throughout [stmt.switch], suggested wording changes:
Remove part of [stmt.switch] p2 and the entire [stmt.switch] p3-5:
Add the following paragraphs as [stmt.switch] p3-5: