How can I check if a type is a pointer?
Paul Backus
snarwin at gmail.com
Sat Jun 25 14:51:49 UTC 2022
On Saturday, 25 June 2022 at 14:18:10 UTC, rempas wrote:
> For example, something like the following:
>
> ```d
> void main() {
> char accepted_type;
> char* non_accepted_type;
>
> if (__traits(isPointer, typeof(accepted_type))) {
> // The type is not accepted
> }
> ```
Use an [`is()` expression:][1]
```d
if (is(typeof(accepted_type) == T*, T))
{
// it's a pointer
}
```
In English, you read this as "if `typeof(accepted_type)` matches
the pattern `T*`, where `T` is a type."
If you want to learn more, I recommend reading [Adam Ruppe's
explanation of `is()` expressions][2].
[1]: https://dlang.org/spec/expression.html#IsExpression
[2]:
https://forum.dlang.org/post/xklcgjaqggihvhctczxx@forum.dlang.org
More information about the Digitalmars-d-learn
mailing list