enum template shorthand and short circuit evaluation

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 10 08:06:08 PDT 2014


On 06/10/14 02:28, Byron via Digitalmars-d-learn wrote:
> Should this work?  It seems like the short circuit booleans are not 
> working:
> 
> enum isPrimitive(T) = isBasicType!T || (isArray!T && isBasicType!
> (ForeachType!T));

[...]

> But this style works:
> 
> template isPrimitive(T) 
> {
>     static if(isBasicType!T || (isArray!T && isBasicType!(ForeachType!T))) 
> {
>         enum isPrimitive = true;
>     } else {
>         enum isPrimitive = false;
>     }
> }

Static-if is special. Outside of static-if [1], code needs to be valid, 
even if it ends up never being executed.

> Should this work?

No. Allowing invalid code just because it will be skipped when the code
executes (either at RT or CT) would create other problems, while only
solving a minor and relatively rare one (the verbosity of static-if).

artur

[1] and a few other contexts, like in template constraints, is-expressions etc.


More information about the Digitalmars-d-learn mailing list