What you do is completely standard C ++ code, although I donβt think it is particularly easy to read or maintain. If we look at the C ++ standard project section 6.4 Selection Operators, the grammar for the switch statement is as follows:
switch ( condition ) statement
Operators
include labels, if, while, for etc ... and section 6.4.2 The switch statement does not create restrictions that might exclude the displayed code.
Case shortcuts are similar to the shortcuts used with goto, which are described in section 6.1 Labeled Designation, but they are limited to use in the switch statement:
Labels and default labels should only be run in switch statements.
and section 6.7 says that we can transfer to a block with some restrictions, such as not bypassing an initialization declaration:
It is possible to pass to the block, but not in such a way as to bypass declarations with initialization. A program that jumps 87 from the point where the variable with the duration of automatic storage is not in the region to the point where it is in the region is poorly formed if the variable is not of a scalar type, class type with trivial default constructor and trivial destructor, cv- a qualified version of one of these types or an array of one of the previous types and is declared without an initializer (8.5).
footnote 87 says:
Passing from the condition of the switch statement to the case label is considered a leap in this regard.
Perhaps one of the most famous and weird uses of the switch statement is to use a Duff device that has a built-in while :
void send( int *to, const int *from, int count) { int n = (count + 7) / 8; switch(count % 8) { case 0: do { *to = *from++;
source share