0

There is a code which needs to be compiled with C++17 as well as with an earlier standards. Compiler: GCC. I've tried to use different techniques for a different standards. Those works fine separately (without if-else: c++17 - with attribute, c++14 - with a comment) , but if I try to use those together, in one if-else, it's failed.

There should no be "break" keyword, this is an expected logic for the case.

case 10:
    if (/* some condition */) {
    // ... some processing 
       break;
    }
    #if __cplusplus >= 201703L
        [[fallthrough]];
    #else
        /* fall through */
    #endif
default:
// ... other processing

The build with c++14 fails with an error like no "else" branch there:

error: this statement may fall through [-Werror=implicit-fallthrough=]

any idea why it happens

amigo421
  • 2,429
  • 4
  • 26
  • 55
  • 1
    Did you try using [the `__attribute__` alternative](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wimplicit-fallthrough)? An earlier question here suggests that the comment must immediately precede the `case` to be effective. It's possible that the intervening `#endif` disqualifies it. – Sam Varshavchik Jul 02 '21 at 11:55
  • yes, gcc-specific attribute works fine, just would like to understand what is wrong with an if-else. do you mean that comment version should be BEFORE the case? if I remove if-else with c++17 attribute and keep the comment on the same place as for now, it works fine – amigo421 Jul 02 '21 at 12:02
  • It should immediately precede case/default. Here you have an intervening `#endif` stuck in between. A simple Google search found [this question](https://stackoverflow.com/questions/45129741/) whose first answer gives more details. – Sam Varshavchik Jul 02 '21 at 12:09
  • Compile fine [Here](https://godbolt.org/z/xMnG84bdo) – Jarod42 Jul 02 '21 at 12:23
  • I see , thank you! then I'll look for the issue in my compilation flags/conditions – amigo421 Jul 02 '21 at 13:42
  • 1
    Why don't you just use the comment for all versions? – ssbssa Jul 02 '21 at 15:02
  • yes, you are right, comment version is a solution for all GCC standards. – amigo421 Jul 03 '21 at 16:13

0 Answers0