www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - compile-time detection of all pointer types in one test

reply DLearner <bmqazwsx123 gmail.com> writes:
Please consider:
```
void main() {

    import std.stdio;

    struct foo {

       int  foo1;
       char foo2;
    }
    foo*   fooptr;

    void*  genptr;


    static if (is(typeof(fooptr) == void*))
              writeln("fooptr is void*");
           else
              writeln("fooptr is not void*");

    static if (is(typeof(fooptr) == foo*))
              writeln("fooptr is foo*");
           else
              writeln("fooptr is not foo*");

    static if (is(typeof(genptr) == void*))
              writeln("genptr is void*");
           else
              writeln("genptr is not void*");
}
```
which produces:
```
fooptr is not void*
fooptr is foo*
genptr is void*
```

Since `void*` variables accept _all_ pointer types, I expected to 
see `fooptr is void*`.

Is there any way of picking up, at compile-time, all pointer 
types in one test?
Jun 11 2023
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
std.traits : isPointer

or

``is(int* : T*, T)``
Jun 11 2023
parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
Just to be clear where the argument goes (from templates parameters ext.):

``is(InputType : T*, T)``
Jun 11 2023
prev sibling next sibling parent reply Andy <andy.pj.hanson gmail.com> writes:
On Sunday, 11 June 2023 at 21:25:21 UTC, DLearner wrote:
 Please consider:
 ```
 void main() {

    import std.stdio;

    struct foo {

       int  foo1;
       char foo2;
    }
    foo*   fooptr;

    void*  genptr;


    static if (is(typeof(fooptr) == void*))
              writeln("fooptr is void*");
           else
              writeln("fooptr is not void*");

    static if (is(typeof(fooptr) == foo*))
              writeln("fooptr is foo*");
           else
              writeln("fooptr is not foo*");

    static if (is(typeof(genptr) == void*))
              writeln("genptr is void*");
           else
              writeln("genptr is not void*");
 }
 ```
 which produces:
 ```
 fooptr is not void*
 fooptr is foo*
 genptr is void*
 ```

 Since `void*` variables accept _all_ pointer types, I expected 
 to see `fooptr is void*`.

 Is there any way of picking up, at compile-time, all pointer 
 types in one test?
Use an `is` expression with a parameter list (https://dlang.org/spec/expression.html#is-parameter-list): void main() { import std.stdio; struct foo {} foo* fooptr; static if (is(typeof(fooptr) == T*, T)) writeln("fooptr is a pointer to a ", T.stringof); else writeln("fooptr is not a pointer"); } As a bonus, you can make use of the pointee type `T`.
Jun 11 2023
parent DLearner <bmqazwsx123 gmail.com> writes:
On Sunday, 11 June 2023 at 21:32:11 UTC, Andy wrote:
[...]
     void main() {
     	import std.stdio;
     	struct foo {}
     	foo* fooptr;
     	static if (is(typeof(fooptr) == T*, T))
     		writeln("fooptr is a pointer to a ", T.stringof);
     	else
     		writeln("fooptr is not a pointer");
     }
Unfortunately, testing more than one variable: ``` void main() { import std.stdio; struct foo1 {int foo1int; char foo1char;} foo1* foo1ptr; struct foo2 {int foo2int; char foo2char;} foo2* foo2ptr; static if (is(typeof(foo1ptr) == T*, T)) writeln("foo1ptr is a pointer to a ", T.stringof); else writeln("foo1ptr is not a pointer"); static if (is(typeof(foo2ptr) == T*, T)) writeln("foo2ptr is a pointer to a ", T.stringof); else writeln("foo2ptr is not a pointer"); } ``` produced ``` static_if_ex05.d(16): Error: declaration `T` is already defined static_if_ex05.d(11): `alias` `T` is defined here ```
Jun 13 2023
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/11/23 5:25 PM, DLearner wrote:
 Is there any way of picking up, at compile-time, all pointer types in 
 one test?
 
What you want is `is(T : void *)` but probably to also catch all constancy flavors, `is(immutable(T) : immutable(void*))` When you use `==` in an `is` expression, it's asking is this *exactly* that type. Using the colon is asking can it *implicitly convert* to that type. -Steve
Jun 12 2023