3d-logic.com - Code, the Universe and Everything…

Example domain paragraphs

Exhaustive switch statements are switch statements that do not have the default case because all possible values of the type in question have been covered by one of the switch cases. Exhaustive switch statements are a perfect match when working with scoped enum types. This can be illustrated by the following code:

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters enum.cpp:12:12: warning: enumeration value 'Yellow' not handled in switch [-Wswitch] switch(c) { ^ enum.cpp:12:12: note: add missing switch cases switch(c) { ^ enum.cpp:17:1: warning: non-void function does not return a value in all control p

Without the exhaustive switch, the compiler would happily compile the program after adding the new enum member because it would be handled by the default case. Even if the default case was coded to throw an exception to help detect unhandled enum members, this exception would only be thrown at runtime which could be too late to prevent failures. In a bigger system or application there could be many switch statements like this and without the help from the compiler it can be hard to find and test all of them

Links to 3d-logic.com (1)