[Bug c++/87951] GCC warns about reaching end of non-void function when all switch is completely handled

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Nov 9 18:52:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87951

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Vitali from comment #0)
> If a function has a single switch statement that handles all enum values &
> returns a value GCC will warn about the function not returning a value
> whereas clang does not.  GCC requires an explicit __builtin_unreachable()
> annotation after the switch. As of C++17 it seems to now be undefined
> behaviour rather than unspecified behaviour for an enum to have a value
> that's not enumerated.

No, that's not what the defect report says. I wish this myth would die.

Outside the range of the enumerated values does not mean not enumerated.
Given enum E { e0=0, e10=7 } there is nothing undefined about E(1) or E(4).

> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1766
> 
> > cat test.c
> 
> enum Enum {
>   A,
>   B,
> };
> 
> int CoverMyBases(enum Enum x) {
> 	switch (x) {
> 		case A:
> 			return 1;
> 		case B:
> 			return 0;
> 	}
> }
> 
> int main(int argc, const char **argv) {
> 	CoverMyBases(A);
> 	CoverMyBases(B);
> 	return 0;
> }
> 
> > g++-8 -Wall --std=c++17 test.c
> 
> test.c: In function 'CoverMyBases':
> test.c:16:1: warning: control reaches end of non-void function
> [-Wreturn-type]
>  }
>  ^

This is what -fstrict-enums is for.


More information about the Gcc-bugs mailing list