TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  CFL#002Checked automatically with code checker

Synopsis:All switch statements shall have a default label as the last case label
Language:C++
Severity Level:2
Category:Control Flow


Description:

Example:

switch (c)
{
case c0:	// fall-through
case c1:
    {
        x;
        break;
    }
case c2:
    {
        y;
        break;
    }
default:	// always 'default' at end, even if no statement follows
    {
        z;	// possibly an assertion that this should not be reached
        break;	// leave break: in case this becomes a non-default case
    }
}

This rule doesn't hold for enumerations if all enum values are explicitly used as case labels in the switch statement. If one of the enum values of the enumeration is missing in the switch body this rule will trigger as well. This is to make sure one doesn't forget the add new enum values to the switch statement in case the enumeration is extended.

enum Color { red, green, blue };
Color r = red;
switch (r)
{
    case red  : std::cout << "red\n";   break;
    case green: std::cout << "green\n"; break;
    case blue : std::cout << "blue\n";  break;
}


Literature References:
Ellemtel Rule 48
ISC++ Rec. 4.5