++ 17 [[fallthrough]]
:
int main(int argc, char **argv) {
switch (argc) {
case 0:
argc = 1;
[[fallthrough]];
case 1:
argc = 2;
};
}
:
g++ -std=c++17 -Wimplicit-fallthrough main.cpp
[[fallthrough]];, GCC :
main.cpp: In function โint main():
main.cpp:5:15: warning: this statement may fall through [-Wimplicit-fallthrough=]
argc = 1;
~~^~~
main.cpp:6:9: note: here
case 1:
^~~~
, , : case ( case 1) , break.
:
#include <cstdlib>
[[noreturn]] void my_noreturn_func() {
exit(1);
}
int main(int argc, char **argv) {
switch (argc) {
case 0:
argc = 1;
break;
case 1:
argc = 2;
}
switch (argc) {
case 0:
argc = 1;
return 0;
case 1:
argc = 2;
}
switch (argc) {
case 0:
argc = 1;
my_noreturn_func();
case 1:
argc = 2;
}
switch (argc) {
case 0:
case 1:
argc = 2;
}
switch (argc) {
case 0:
argc = 1;
case 1:
argc = 2;
}
switch (argc) {
case 0:
argc = 1;
__attribute__ ((fallthrough));
case 1:
argc = 2;
}
switch (argc) {
case 0:
if (argv[0][0] == 'm') {
[[fallthrough]];
} else {
return 0;
}
case 1:
argc = 2;
}
}
, GCC , - [[fallthrough]]; break return.
, , GEM5 :
#if defined __has_cpp_attribute
#if __has_cpp_attribute(fallthrough)
#define MY_FALLTHROUGH [[fallthrough]]
#else
#define MY_FALLTHROUGH
#endif
#else
#define MY_FALLTHROUGH
#endif
: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
GCC 7.4.0, Ubuntu 18.04.
C : GCC 7, -Wimplicit-fallthrough , ?