How to get compatible symbol names and runtime typeid names for templated classes?

Arafel er.krali at gmail.com
Tue May 3 12:33:43 UTC 2022


On 3/5/22 12:48, bauss wrote:
> This is where compile-time has its limits compared to runtime type 
> creation, because templates only live during compile-time then it isn't 
> really that easy to do something like this, where it would be trivial in 
> other languages like C#.

That's something I don't really get. I totally understand that you can't 
instantiate the template during runtime, but why can't already 
instantiated classes be registered just like non-templated ones?

I tried the following snippet, and couldn't find C!int.C anywhere, 
although it **must** be there: I can get the `TypeInfo_Class` object, so 
I can clearly create new instances at runtime:

```d
import std.stdio : writeln;

class C(T) {}
class D {}

void main() {
     auto c = new C!int();
     auto c2 = typeid(c).create();
     auto d = new D();
     writeln(typeid(c).name);
     writeln(typeid(c2).name);
     writeln(typeid(d).name);
     writeln("----");
     writeln;
     writeln;
     foreach (m; ModuleInfo) {
         if (m) {
             writeln(m.name);
             writeln("--------------------");
             foreach (c; m.localClasses) {
                 if (c) {
             		writeln(c.name);
                 }
             }
             writeln;
         }
     }
}
```



More information about the Digitalmars-d-learn mailing list