what is the different bettwen typeid and .classinfo?

Steven Schveighoffer schveiguy at gmail.com
Mon Mar 30 22:40:55 UTC 2020


On 3/30/20 5:17 PM, Pham wrote:
> On Monday, 30 March 2020 at 18:20:00 UTC, Steven Schveighoffer wrote:
>> I don't really understand the question about "nothrow".
>>
> 
> void main()
> {
>      static class Foo { int i; }
> 
>      static bool checkFoo() nothrow
>      {
>          // error when compile
>          //return typeid(Foo) == Foo.classinfo;
> 
>          // ok when compile
>          return typeid(Foo) is Foo.classinfo;
>      }
> 
>      checkFoo();
> }

In this case, the difference is == vs. `is`, not classinfo vs typeid:

typeid(Foo) == typeid(Foo); // throws

Foo.classinfo == Foo.classinfo; // throws

typeid(Foo) is typeid(Foo); // nothrow

Foo.classinfo is Foo.classinfo; // nothrow

`is` between class instances means "is this the same instance". It is a 
simple comparison of two pointers basically.

== between classes means calling object.opEquals [1], which is not 
nothrow (or nogc, or pure).

In fact, they should be the same instance, because typeid and classinfo 
do the same thing.

-Steve

[1] https://dlang.org/phobos/object.html#.opEquals


More information about the Digitalmars-d-learn mailing list