assert and static assert and code generatio

Cecil Ward cecil at cecilward.com
Wed Jun 14 15:23:34 UTC 2023


On Wednesday, 14 June 2023 at 14:53:21 UTC, Steven Schveighoffer 
wrote:
> On 6/14/23 9:00 AM, Cecil Ward wrote:
>> [...]
>
> A static assert is telling the compiler if the condition isn't 
> true at compile time, to *halt compilation*. That is, static 
> asserts have no bearing on semantic meaning other than to say 
> "this isn't compilable". By definition, the compiler knows 
> whether the assert is true or not, so there is no "hint" here 
> for better code generation that it couldn't already gain from 
> it's own knowledge.
>
> [...]

Many thanks for the tip about traits. I had used that or 
something similar - memory fails me - to check on whether some 
expression was legal in some sense, in a condition on a template, 
but I have not used it like this, so very valuable thanks.

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.


More information about the Digitalmars-d mailing list