Why does this code only work with `T` and not `typeof(T)`?

thebluepandabear therealbluepandabear at protonmail.com
Wed Jan 4 04:11:18 UTC 2023


I am using the CSFML D bindings, and I am creating my own `draw` 
template function.

I first check that the object passed in is of the appropriate 
type, and if it is, I call the appropriate function:

```D
template isDrawable(T) {
     enum isDrawable = is(T == sfCircleShape*) || is(T == 
sfRectangleShape*) || is(T == sfText*) || is(T == sfSprite*);
}

void sfRenderWindowExt_draw(T)(sfRenderWindow* renderWindow, T 
obj) {
     static assert(isDrawable!T, format("Cannot call any draw 
method on type %s", T.stringof));

     if (is(T == sfCircleShape*)) {
         
renderWindow.sfRenderWindow_drawCircleShape(cast(sfCircleShape*)obj, null);
     } else if (is(T == sfRectangleShape*)) {
         
renderWindow.sfRenderWindow_drawRectangleShape(cast(sfRectangleShape*)obj, null);
     } else if (is(T == sfText*)) {
         renderWindow.sfRenderWindow_drawText(cast(sfText*)obj, 
null);
     } else if (is(T == sfSprite*)) {
         
renderWindow.sfRenderWindow_drawSprite(cast(sfSprite*)obj, null);
     }
}
```

For some reason, if I replace the `isDrawable` template with the 
following (using `typeof`), the code does not compile:

```D
template isDrawable(T) {
     enum isDrawable = is(typeof(T) == sfCircleShape*) || 
is(typeof(T) == sfRectangleShape*) || is(typeof(T) == sfText*) || 
is(typeof(T) == sfSprite*);
}
```

This is really strange, as I assumed that the two pieces of code 
were essentially the same.

Any help as to why this is the case would be appreciated.


More information about the Digitalmars-d-learn mailing list