When I compile the following code at c++11 standard, it works fine with clang, and also with gcc, but gcc (all versions that I tested 4.8.2, 4.9.2, 5.1.0) gives a warning:
#include <iostream>
enum class FOO { A, B, C };
const char * bar(FOO f) {
switch (f) {
case FOO::A:
return "A";
case FOO::B:
return "B";
case FOO::C:
return "C";
}
}
int main() {
unsigned int x;
std::cin >> x;
FOO f = static_cast<FOO>(x % 3);
std::cout << bar(f) << std::endl;
}
The warning is -Wreturn-type:
main.cpp: In function ‘const char* bar(FOO)’:
main.cpp:14:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1plus: all warnings being treated as errors
I still get the warning even with -O2 or -O3 optimizations -- does this mean that even at high optimization levels, gcc cannot dead-code eliminate the 'end' of the function?
Notably, it doesn't give me the warning about unhandled switch cases.
Is there a good way to suppress this warning locally within such a function, or is the only way to suppress this to disable the warning generally?
Aucun commentaire:
Enregistrer un commentaire