What is the proper way to outline static-if-conditions ?

drug drug2004 at bk.ru
Sun Oct 10 15:15:51 UTC 2021


On 10.10.2021 18:01, Elmar wrote:
> 
> Well, I just wondered why your code would compile and mine wouldn't. The 
> `version(all)` variant will not compile on my computer with `rdmd` 
> because `PointerTarget` only allows pointers.

It depends on compiler version. This variant is compiled on version 
2.092.1 and above

> 
> But the 2nd one will compile. The `is()` expression catches the 
> compilation error which is nice. This is sufficient:
> 
> ```d
> enum isPointedStaticArray(T) = is(PointerTarget!T : P[N], P, size_t N);
> ```
> 
> It would be nice if one could use pattern-matching for it in D. Is this 
> possible?
> 
> ```d
>      enum isPointedStaticArray(X : P*, P) = 
> .isStaticArray!(PointerTarget!X);
>      enum isPointedStaticArray(X : else) = false;
> ```
> 

As I know it's impossible, but you can use a regular template:
```d
template isPointedStaticArray(T)
{
     static if (isPointer!T)
         enum isPointedStaticArray = isStaticArray!(PointerTarget!T);
     else
         enum isPointedStaticArray = false;
}
```
https://run.dlang.io/is/lR7feP
this compiles from 2.086.1 and above


More information about the Digitalmars-d-learn mailing list