Many times when writing switch statements, your code will handle every possible case that the variable controlling
the switch could take. For example, if you have a MFC dialog with a combo box or radio button group, with an integer
member variable hooked up to hold the user's selection, you might have a switch that takes an action based on that
variable's value.
In switches like that example, you know that under normal circumstances, the code will never reach the default
section in the switch. If the code ever should hit the default section, it would indicate either a bug
in either the switch or the UI handling code. This is the kind of thing that asserts are designed to catch - conditions
that should never occur if the code is working correctly.
I created this debugging macro, which I imaginitavely called DEFAULT_UNREACHABLE
,
that you can put at the end of your switches, to make sure that any bugs that are introduced which would cause
execution to pass through the switch unhandled, are caught instead. The macro lets you know that something is wrong
by generating an assertion failure. The macro has three parts:
- Debug builds: Forces an assertion failure so MSVC will drop you in the debugger right at the spot where the
bug showed up.
- Release builds for VC 6 and later: Contains an
__assume
statement as an
optimization hint to the compiler. According to the VC docs, this reduces the size of the compiled code for the
switch.
- Release builds for VC 5 and earlier: Does nothing.
You may be wondering, "Why doesn't the release version do anything? Isn't the end result the same as without
the macro?" That is indeed true, but remember that the debug version is the workhorse here. When you test
your debug builds, you will catch and fix the bugs flagged by the macro, making sure that the code never gets to
the default sections. That means that the release builds don't need any code in the default sections, because (say
it with me) the code will never get there.
Without further ado, here is the code for the macro, and a sample showing how to use it.
#ifdef _DEBUG
#define DEFAULT_UNREACHABLE default: ASSERT(0); break
#elif _MSC_VER >= 1200
#define DEFAULT_UNREACHABLE default: __assume(0); break
#else
#define DEFAULT_UNREACHABLE default: break
#endif;
Sample usage: Imagine a dialog with radio buttons for selecting the day of the week. There is a variable m_nDay
hooked up to the radio buttons.
void CMyDialog::OnOK()
{
UpdateData();
switch ( m_nDay )
{
case 0: case 6:
break;
case 1: case 2: case 3:
break;
case 4: case 5:
break;
DEFAULT_UNREACHABLE;
}
I, as the author of the code, know that m_nDay should never contain a value other than 0 through 6, and if it ever does, it indicates a bug. So I put the
DEFAULT_UNREACHABLE
macro in the switch, and it will alert me if that situation
ever happens.
As a final note, I should mention that the latest winnt.h in the SDK has a DEFAULT_UNREACHABLE
macro, but it is not as good as the one here. The SDK version just reduces
down to an __assume
statement, and has no provision for aiding in debugging.