compile-time detection of all pointer types in one test

DLearner bmqazwsx123 at gmail.com
Tue Jun 13 22:22:23 UTC 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
```


More information about the Digitalmars-d-learn mailing list