assume, assert, enforce, @safe

ponce via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 31 06:49:49 PDT 2014


On Thursday, 31 July 2014 at 11:01:56 UTC, Marc Schütz wrote:
> On Thursday, 31 July 2014 at 10:24:07 UTC, ponce wrote:
>> If I write:
>>
>> ---
>> switch(expr())
>> {
>> case 0: doIt();
>> case 1: doThat();
>> default:
>>  assert(0);
>> }
>> ---
>>
>> Will the optimizer be able to remove the default: case?
>
> Assuming fall-through (`goto case`), not only the default case. 
> The entire switch could be removed, under the condition that 
> the compiler can prove that neither `expr()`, `doIt()`, nor 
> `doThat()` throws, even if they have side effects. And maybe 
> even the entire function, and all functions that call it, 
> depending on how exactly the control flow is.

Ok my example was wrong, I meant:

---
switch(expr())
{
case 0: doIt(); break;
case 1: doThat(); break;
default:
      assert(0);
      break;
}
---


More information about the Digitalmars-d mailing list