Thread overview
compile-time detection of all pointer types in one test
Jun 11, 2023
DLearner
Jun 11, 2023
Andy
Jun 13, 2023
DLearner
June 11, 2023

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?

June 12, 2023
std.traits : isPointer

or

``is(int* : T*, T)``
June 11, 2023

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.

June 12, 2023
Just to be clear where the argument goes (from templates parameters ext.):

``is(InputType : T*, T)``

June 12, 2023

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

June 13, 2023

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