Hi, On 2018-12-18 23:17:54 -0500, Tom Lane wrote: > Andres Freund <and...@anarazel.de> writes: > > It'd be nice if there were an easy way to write a switch() where the > > compiler enforces that all enum values are checked, but still had the > > possibility to have a 'default:' block for error checking... I can't > > quite come up with a good approach to emulate that though. > > Yeah, that would sure make things better. I was considering > something like > > switch (enumvalue) > { > case A: ... > case B: ... > ... > > #ifndef USE_ASSERT_CHECKING > default: > elog(ERROR, ...); > #endif > } > > so that you get the runtime protection in production builds but not > debug builds ... except that yes, you really want that protection in > debug builds too. Maybe the #if could be on some other symbol so that > the default: is normally enabled in all builds, but we have some > lonely buildfarm animal that disables it and builds with -Werror to > get our attention for omitted cases?
Yea, that's the best I can come up with too. I think we'd probably want to have the warning available during development mainly, so I'm not sure the "lonely buildfarm animal" approach buys us enough. There's -Wswitch-enum which causes a warning to emitted for missing enum cases even if there's default:. Obviously that's not generally usable, because there's a lot of cases where intentionally do not want to be exhaustive. We could just cast to int in those, or locally disable the warnings, but neither sounds particularly enticing... Greetings, Andres Freund