Type constraint

Joel joelcnz at gmail.com
Wed Oct 4 01:46:42 UTC 2023


On Tuesday, 3 October 2023 at 17:42:51 UTC, Jonathan M Davis 
wrote:
> On Tuesday, October 3, 2023 8:35:31 AM MDT Joel via 
> Digitalmars-d-learn wrote:
>> [...]
>
> Yeah. static if will compile in the code in that branch based 
> on whether the condition is true, whereas if without the static 
> will branch at runtime, with both branches being compiled in. 
> So, if you want to be changing what code is being compiled in, 
> you need static if, not if.
>
> if(isIntegral!T)
>
> will compile just fine, but it'll just end up being either
>
> if(true)
>
> or
>
> if(false)
>
> and the code within that branch will be compiled in regardless 
> (and potentially result in compiler errors if it doesn't work 
> with the type that that the template is being instantiated 
> with). So, you usually want to use static if with compile-time 
> tests and not if.
>
> - Jonathan M Davis

I think the if without static is still static, since it's part of 
the function name part, or so (outside of the curly bracket 
scope).


More information about the Digitalmars-d-learn mailing list