-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Description
In this compiler explorer experiment, GCC warns "warning: control reaches end of non-void function", while Clang does not. Pasting it here for self-containedness:
enum class B : int {
B1, B2
};
int f(B b) {
switch(b) {
case B::B1: return 1;
case B::B2: return 2;
}
}In this instance, GCC is making an important point: the function f cannot assume that its parameter of enum type has a value that is one of the enumerator values, because this enumerator has a a fixed underlying type (the : int in the enum declaration) and the standard says that in that case, any integer of that underlying type may be casted to this enum type: https://eel.is/c++draft/expr.static.cast#9
Currently, GCC always warns regardless of having a specified underlying type in the enum declaration ; Clang never warns. If my understanding of the spec is correct then the reachability of the end of the function (and therefore, the rationale for generating a warning) is when there is no specified underlying type.