Thread overview
Extract typeinfo
Mar 05, 2014
Namespace
Mar 05, 2014
Adam D. Ruppe
Mar 05, 2014
Namespace
March 05, 2014
Is there a way to extract the correct typeinfo of an array if I only have the pointer?

Code:
----
import std.stdio;

struct Foo {
	int i;
}

void foo(void* p) {
    // ???
}

void main() {
    Foo[] fs;
    fs ~= Foo();

    foo(fs.ptr);
}
----

Now that we have only the untyped void*: Is there a way how I can extract the right typeinfo? Something to rebuild the correct type out of the adress.
March 05, 2014
On Wednesday, 5 March 2014 at 19:15:12 UTC, Namespace wrote:
> Is there a way to extract the correct typeinfo of an array if I only have the pointer?

Nope, typeinfos are only stored in runtime on classes. All other types get it only through the static info.

Consider the following:

char[10] a;

char[] b = a[3..5];// from the middle of it

void* c = b.ptr;


Even if the info was stored with a, the slice would have no idea where to find it, so you couldn't get it from the pointer either.


The way druntime does it with void pointers is to pass the typeinfo too:

void useArray(void[] a, TypeInfo a_type);
March 05, 2014
On Wednesday, 5 March 2014 at 19:21:39 UTC, Adam D. Ruppe wrote:
> On Wednesday, 5 March 2014 at 19:15:12 UTC, Namespace wrote:
>> Is there a way to extract the correct typeinfo of an array if I only have the pointer?
>
> Nope, typeinfos are only stored in runtime on classes. All other types get it only through the static info.
>
> Consider the following:
>
> char[10] a;
>
> char[] b = a[3..5];// from the middle of it
>
> void* c = b.ptr;
>
>
> Even if the info was stored with a, the slice would have no idea where to find it, so you couldn't get it from the pointer either.
>
>
> The way druntime does it with void pointers is to pass the typeinfo too:
>
> void useArray(void[] a, TypeInfo a_type);

Ah thank you. :)