Any weird switch / default purpose in this code?

I am migrating code from C to C ++ and found this code:

if(ErrorCode >= SOME_CONSTANT)
{
    Status = RETVAL_OK;

    switch ( ErrorCode )
    {
        default:
            Status = RETVAL_FAILED;
            break;
    }
}

This code generates a compilation warning:

warning C4065: switch statement contains 'default' but no 'case' labels

Question: is there any purpose of the switch statement (which I did not understand) or is it just cool code?

That is, is there any reason (when compiling for ANSI C or C ++) not to write it like this?

if(ErrorCode >= SOME_CONSTANT)
     Status = RETVAL_FAILED;

EDIT . To answer all your questions:

The code is not intended to be extended: it was the last release of the module, which was delivered four years ago (since then it has not been affected, so I am inclined to believe that it is cool).

case ( ( switch/default a, ). , , .

.

+3
6

, : 1) 2) , , . if

+9

, - , , . , , .

+3

, , , , case.

, . , , .

+3

Crufty.

+2

, RETVAL_FAILED. , , - .

+2

If it ErrorCodeis an enumeration, you might want to check the other elements in the enumeration and use them in the switch statement.

Alternatively, I think the source program had a different function call

ErrorCode = Func();

Now it makes sense to have

switch(ErrorCode)
{
case enum1:
break;
case enum2:
break;
default:
break;
}

This approach is used when you have to perform several tasks in a function, and the return value is shared.

0
source

Source: https://habr.com/ru/post/1752285/


All Articles