Is it safe to use 'is' to compare types?

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 4 13:30:51 PST 2016


On Friday, 4 March 2016 at 15:18:55 UTC, Steven Schveighoffer 
wrote:
> On 3/3/16 6:58 PM, Yuxuan Shui wrote:
>> On Thursday, 3 March 2016 at 23:51:16 UTC, Adam D. Ruppe wrote:
>>> On Thursday, 3 March 2016 at 23:46:50 UTC, Yuxuan Shui wrote:
>>>> Will typeid(a) is typeid(b) yield different results than 
>>>> typeid(a) ==
>>>> typeid(b)?
>>>
>>> No. Indeed, opEquals on TypeInfo just calls is itself.
>>
>> But opEquals also has extra comparison:
>>
>>          auto ti = cast(const TypeInfo)o;
>>          return ti && this.toString() == ti.toString();
>>
>> This makes me feel they are not the same.
>
> In some cases, for instance using DLLs, the TypeInfo for an 
> object allocated in one way may be identical, but be a 
> different instance from the TypeInfo allocated in another way. 
> This is why the string comparison occurs.
>
> Note that comparing ANY object will first check if they are the 
> same instance before calling any functions (this is in 
> object.opEquals)
>
> -Steve

Thanks for answering. But I still don't understand why TypeInfo 
would need to be allocated. Aren't typeid() just returning 
references to the __DxxTypeInfo_xxxx__initZ symbol?


More information about the Digitalmars-d-learn mailing list