On 10/09/2014 12:21 AM, Anirban Chakraborty wrote:
> Alas, using a default case means the compiler can no longer tell
you
> about missed cases. I wrote my example to intentionally spell out ALL
> cases without a default, in order to ensure the compiler will loudly
> warn if we add another enum in the future.
I understood the intention. However, I was aiming for a compact code. If
we add additional enums for network type, we should be adding those types
in the above switch provided the new interface type supports bandwidth. I
do not know if we should prevent future coding error by elaborating the
current code. Forgive me, as I do not know the coding convention for
libvirt. If this is the standard practice, I¹ll change the above switch as
per your wish.
Hmm - we really ought to add a segment to HACKING that mentions the trick.
The thing to remember is that gcc is able to warn for any enum used as a
switch statement where there are only 'case FOO:' labels and no
'default:' label, and where not all the enum values are covered. So
code-maintenance wise, if you have an enum that might grow a value in
the future, and want to make sure any addition of a new value gets
automatically flagged as to all places of code that need to think about
behavior related to that new value, then using a switch with no default
lets the compiler help you find those places.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library
http://libvirt.org