Is there a way to use Object.factory with templated classes? Or some way to construct templated classes given RTTI of an instance?

Chad Joan chadjoan at gmail.com
Thu Sep 27 18:48:31 UTC 2018


On Thursday, 27 September 2018 at 14:23:48 UTC, Steven 
Schveighoffer wrote:
> On 9/27/18 10:20 AM, Steven Schveighoffer wrote:
>> typeid sometimes gives you a more derived type than TypeInfo. 
>> Including for classes and structs.
>> 
>> In the past, .classinfo gave you a different thing than 
>> typeid(obj), but now it is the same thing:
>> 
>> 
>>      auto obj = new Object;
>>      // classinfo and typeid are the same object
>>      assert(obj.classinfo is typeid(obj));
>>      // and the same type
>>      static assert(is(typeof(obj.classinfo) == 
>> typeof(typeid(obj))));
>> 
>> I wouldn't use classinfo any more, I generally use typeid.
>
> I should add that typeid does give you the derived 
> TypeInfo_Class, not the concrete one.
>
> that is:
>
> Object obj; // = null
>
> //typeid(obj); // segfault, can't dereference null pointer
>
> class C {}
>
> obj = new C;
>
> static assert(is(typeof(obj) == Object));
>
> writeln(typeid(obj)); // C, not Object
>
> So it really is a drop-in replacement for classinfo. I think 
> classinfo is still there to avoid breaking existing code that 
> uses it.
>
> -Steve

Interesting!  That's yet another thing I hadn't realized had 
changed.  Good to know.


More information about the Digitalmars-d-learn mailing list