what is the different bettwen typeid and .classinfo?

Steven Schveighoffer schveiguy at gmail.com
Mon Mar 30 18:20:00 UTC 2020


On 3/30/20 1:06 PM, Pham wrote:
> On Monday, 30 March 2020 at 15:15:08 UTC, Steven Schveighoffer wrote:
>> They are both the same. Historically they were different (ClassInfo 
>> was its own type different from TypeInfo_Class). I would recommend 
>> typeid for future-proof code (it's possible, however unlikely, that 
>> .classinfo at some point goes away).
>>
> 
> Will it be the same if using "is", the reason is for function that use 
> "nothrow" attribute?
> if (cc.classinfo is typeid(CC))

It should be identical.

For fun I did an AST dump for this code:

void main()
{
     Object o;
     auto ti = typeid(o);
     auto ci = o.classinfo;
}

And this is what I get:

void main()
{
	Object o = null;
	TypeInfo_Class ti = typeid(o);
	TypeInfo_Class ci = **o;
	return 0;
}

So it looks like typeid(o) is not lowered to something, but o.classinfo 
is lowered to **o (the first field in an object is the TypeInfo).

I would say that this is going to reduce to the same thing in both 
cases, but I find it interesting that the AST is different.

I don't really understand the question about "nothrow".

-Steve


More information about the Digitalmars-d-learn mailing list