assert and static assert and code generatio

Cecil Ward cecil at cecilward.com
Thu Jun 15 21:32:33 UTC 2023


On Wednesday, 14 June 2023 at 15:53:01 UTC, Steven Schveighoffer 
wrote:
> On 6/14/23 11:23 AM, Cecil Ward wrote:
>
>> What if I say something like assert( x < 100 ); and let’s say 
>> I’m giving the compiler information in the maximum conceivable 
>> range of values, 0…99, so the compiler could generate better 
>> code as it now knows that the expression x < 200 is false and 
>> let’s say that the assert is the only source of information 
>> about the value range.
>
> Yes, err.... if x < 100, then x is surely < 200? But I think 
> that's just a typo. That's the kind of thing that asserts can 
> do, even if the assert is not generated into code.
>
> -Steve

It wasn’t a typo. I intended to give an second expression that is 
trivially always true. The point was that later if statements, 
even if their test is not identical to the test in the assert, 
can be removed sometimes.

To test things out, I wrote an assume() routine which takes an 
argument that is a bool. It uses a special GCC builtin and can 
make later if statements go away. It worked well and seems 
sufficiently valuable that I will try to use it a lot in future. 
The assume statement is like an assert.


More information about the Digitalmars-d mailing list