Compile time foreach with switch

Nick Treleaven via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 22 04:56:31 PDT 2017


On Friday, 21 April 2017 at 19:17:32 UTC, Adam D. Ruppe wrote:
> Then realize that it does exactly the same thing when it is 
> inside a switch... it always breaks the innermost thing, which 
> happens to be this loop. (note that cases are not `breaked`, 
> the switch is.)

By coincidence I ran into this issue recently and assumed that 
break should break the switch too (and I hadn't read the 
boost::hana thread). Thanks for explaining, I thought it was 
probably a compiler bug and hadn't gotten round to investigating 
further.

So the compiler generates something like this:

	int s = 0;
	switch (s) {
		case 0:
			writeln("matched ");
		default:
			writeln("no match");
			break;
	}

If I compile the above I get:
Deprecation: switch case fallthrough - use 'goto default;' if 
intended

IMO the compiler should issue this warning with the OP's code.


More information about the Digitalmars-d-learn mailing list