Embedding a case label in an if…else statement

后端 未结 2 661
既然无缘
既然无缘 2021-01-05 07:17

G++ accepts this code and it behaves as I\'d expect it to:

#include 

void example (int value, bool condition) {

  switch (value) {
  case 0:         


        
2条回答
  •  庸人自扰
    2021-01-05 07:42

    As far as C++ is concerned (draft N3936):

    • case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels.
    • Usually, the substatement that is the subject of a switch is compound and case and default labels appear on the top-level statements contained within the (compound) substatement, but this is not required.(§ 6.4.2 - 6)
    • It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. (§ 6.7 - 3)

    What you are doing is technically ok, of course that doesn't mean you should.

提交回复
热议问题