www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I get the scalar type of a pointer to pointer (and in even

reply rempas <rempas tutanota.com> writes:
Let's see the following code:

```d
void test_fn(T)(T val) {
   pragma(msg, "The type of the pointer is: " ~ 
typeof(*val).stringof);
}

extern (C) void main() {
   int* int_ptr = cast(int*)0x1037;
   char* char_ptr = cast(char*)0x1037;

   test_fn(int_ptr);
   test_fn(char_ptr);
}
```

This function takes a templates argument that can be any 
(pointer) type and shows the scalar type of the pointer (at 
compile time). I'm using the "typeof" operator and I'm 
referencing the pointer so I can get the scalar type. This will 
work great for single pointer values but what about pointers to 
pointers like: `int****`? Is there a way that I would be able to 
always get the scalar type of a pointer regardless of how many 
levels it is? As always, I'm searching for a solution that will 
work in `BetterC`.
Mar 11 2023
parent reply ag0aep6g <anonymous example.com> writes:
On 11.03.23 13:49, rempas wrote:
 but what 
 about pointers to pointers like: `int****`? Is there a way that I would 
 be able to always get the scalar type of a pointer regardless of how 
 many levels it is? As always, I'm searching for a solution that will 
 work in `BetterC`.
alias Foo(T : U*, U) = Foo!U; alias Foo(T) = T; static assert(is(Foo!(int*) == int)); static assert(is(Foo!(int**) == int)); static assert(is(Foo!(int***) == int)); static assert(is(Foo!(int****) == int));
Mar 11 2023
parent reply rempas <rempas tutanota.com> writes:
On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote:
 alias Foo(T : U*, U) = Foo!U;
 alias Foo(T) = T;
 static assert(is(Foo!(int*) == int));
 static assert(is(Foo!(int**) == int));
 static assert(is(Foo!(int***) == int));
 static assert(is(Foo!(int****) == int));
Wait, but "Foo" is defined twice and it doesn't let me compile...
Mar 11 2023
parent reply ag0aep6g <anonymous example.com> writes:
On 11.03.23 14:22, rempas wrote:
 On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote:
 alias Foo(T : U*, U) = Foo!U;
 alias Foo(T) = T;
 static assert(is(Foo!(int*) == int));
 static assert(is(Foo!(int**) == int));
 static assert(is(Foo!(int***) == int));
 static assert(is(Foo!(int****) == int));
Wait, but "Foo" is defined twice and it doesn't let me compile...
You're doing something wrong then. Don't put the Foo declarations in a function or unittest.
Mar 11 2023
parent rempas <rempas tutanota.com> writes:
On Saturday, 11 March 2023 at 13:37:26 UTC, ag0aep6g wrote:
 On 11.03.23 14:22, rempas wrote:
 On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote:
 alias Foo(T : U*, U) = Foo!U;
 alias Foo(T) = T;
 static assert(is(Foo!(int*) == int));
 static assert(is(Foo!(int**) == int));
 static assert(is(Foo!(int***) == int));
 static assert(is(Foo!(int****) == int));
Wait, but "Foo" is defined twice and it doesn't let me compile...
You're doing something wrong then. Don't put the Foo declarations in a function or unittest.
Oh, so it tries to re-define them if I put them inside a function! Thank you brother, works great! Have a great day!
Mar 11 2023