checkedint call removal
John Colvin via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jul 28 05:53:17 PDT 2014
On Monday, 28 July 2014 at 12:52:07 UTC, John Colvin wrote:
> On Monday, 28 July 2014 at 12:08:39 UTC, Daniel Murphy wrote:
>> "John Colvin" wrote in message
>> news:iguetbdxlyilavlizqry at forum.dlang.org...
>>
>>> To what extent can a compiler use assertions? Can it work
>>> backwards from an assert to affect previous code?
>>>
>>> void foo(int a)
>>> {
>>> enforce(a & 1);
>>> assert(a & 1);
>>> }
>>
>> The assert is dead code, because it will never be reached if
>> (a & 1) is false.
>>
>>> void bar()
>>> {
>>> assert(a & 1);
>>> enforce(a & 1);
>>> }
>>
>> The throw inside enforce is dead code, because it will never
>> be reached if (a & 1) is false.
>>
>> The compiler is free to remove dead code, because it doesn't
>> change the program's behaviour.
>
> Ok. What about this:
>
> int c;
>
> void foo(int a)
> {
> if(a < 0) c++;
> assert(a > 0);
> }
>
> I presume that cannot be optimised away entirely to:
>
> void foo(int a) {}
>
> ?
sorry, I mean
void foo(int a)
{
assert(a > 0);
}
of course you can't optimise away the check.
More information about the Digitalmars-d
mailing list