www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I check if a type is a pointer?

reply rempas <rempas tutanota.com> writes:
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
   }

  else { /* The type is not a pointer so its accepted */ }

   if (__traits(isPointer, typeof(non_accepted_type))) {
     // The type is not accepted
   }

  else { /* The type is not a pointer so its accepted */ }
}
```

In that example, the first comparison will execute the second 
branch and for the second comparison,
it will execute the first branch. Of course, this trait doesn't 
exist I used an example to show what I want
to do. Any ideas?
Jun 25 2022
next sibling parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Saturday, 25 June 2022 at 14:18:10 UTC, rempas wrote:
 In that example, the first comparison will execute the second 
 branch and for the second comparison,
 it will execute the first branch. Of course, this trait doesn't 
 exist I used an example to show what I want
 to do. Any ideas?
I guess you can look at the source code for https://dlang.org/phobos/std_traits.html#isPointer
Jun 25 2022
parent rempas <rempas tutanota.com> writes:
On Saturday, 25 June 2022 at 14:32:27 UTC, Ola Fosheim Grøstad 
wrote:
 I guess you can look at the source code for

 https://dlang.org/phobos/std_traits.html#isPointer
Thank you! Nice and quick ;) For anyone interested, here's the source code: ```d enum bool isPointer(T) = is(T == U*, U) && __traits(isScalar, T); ``` Have a nice day my friend!
Jun 25 2022
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
Jun 25 2022
parent rempas <rempas tutanota.com> writes:
On Saturday, 25 June 2022 at 14:51:49 UTC, Paul Backus wrote:
 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
Thank you too for this example! Have a nice day my friend!
Jun 25 2022