switch(0)
will always execute the block of code associated with the case 0:
block; still, here there's no actually executed code - both cases are empty.
The point here is to make the compiler angry at compile time if the asserted expression (a
) is not verified: in this case, the expanded macro will have two case 0:
- the one provided explicitly, and the one that uses the result of the asserted expression (so, 0 in case it failed); this results in a switch
with two identical case
, which is not allowed and makes the compiler stop with an error at compile time.
This will also fail if the passed expression is not a constant evaluated at compile time (as you cannot have runtime-determined case
values), which is also expected from a static_assert
.