[program_options] Compiler warning in 1.32 release

/home/jody/boost_1_32_0/libs/program_options/src/cmdline.cpp: In member function `void boost::program_options::detail::cmdline::clear_error()': /home/jody/boost_1_32_0/libs/program_options/src/cmdline.cpp:732: warning: ` boost::program_options::invalid_command_line_syntax::kind_t re' might be used uninitialized in this function In this case, the enumeration "re" of type kind_t is not initialized, and will hold "any" value. The switch does not set the value in the default section, and then an exception is thrown with the uninitialized "re" variable.

Hi Jody,
The problem is that gcc is too smart. Switch does not handle a single enumerator --- 'ed_success', very reasonable. It's handled before the switch. Even if I add 'ed_success' case, it still report the warning. So, I've worked this around by adding 'return' in default case. The change is committed. Thanks for the report, Volodya

On Tue, 23 Nov 2004 10:34:33 +0300 Vladimir Prus <ghost@cs.msu.su> wrote:
Hmmm... What do you mean by "Switch does not handle a single enumerator --- 'ed_success', very reasonable?" Also, is a "return" what you really want here? If you have an error condition that you do not recognize, then this should at least throw a logic_error, if not some other. Maybe adding something like "unknown" to invalid_command_line_syntax::kind_t and then initializing "re" to "unknown" would still allow you to fall out of the switch and still throw the exception, with a kind_t of "unknown." Better than just returning, IMO. Also, I have found beneficial when "switching" on an enumeration, to leave out the default case all together. Some compilers will then emit a warning if you forgot to handle a case, or if the enumeration changes and a "new" addition is not handled in the switch.

Jody Hagins wrote:
I mean that switch handles all enumerators, except 'ed_success'. This one indicates no error, so should not be handled in a switch which returns error code. This value is handled with if (e == e_success) return; before the switch
The best approach would be to add "ed_success" to the switch...
...... but gcc still complains about use of uninitialized var. The only solution I have is: - initialize 'res' to some value - add 'ed_success' to switch - remove 'default' This way, if I add a new enumerator compiler will remind to add it to switch. Anyway, I suspect that code will get rewritten some day. - Volodya
participants (2)
-
Jody Hagins
-
Vladimir Prus