Put code before or after break in a switch case?

I use break on default because it's said to be good practice. But, should I put this final function call (where to go next) after or before the break? Is this wrong for some reason?

1
2
3
4
5
6
default:
  cout << "\n\n""ERROR: no good""\n\n";
  this_thread::sleep_for(chrono::milliseconds(2000));
  main();
  break;
}
break sends control to the end of the innermost loop or switch. Anything you want to happen inside the switch needs to happen before you leave.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
void f() { std::cout << "f\n"; }

int main()
{
  int x = 24;
  switch (x) 
  { 
  default:
    break; // go directly to line 12
    f(); // not reachable; will never be called
  }
}


Is this wrong for some reason?

Yes.

It is forbidden to call the main function. Some compilers may assume that it will be called exactly once.

The ISO C++ standard, the technical document that defines what is valid C++, has this to say:
[basic.start.main]
The function main shall not be used in a program. [...]

http://eel.is/c++draft/basic.start.main#3.sentence-1
Last edited on
Topic archived. No new replies allowed.