Fastest way to check if the bottom-class of a class reference is any of a set of classes

Per Nordlöw per.nordlow at gmail.com
Tue Dec 17 20:53:10 UTC 2019


On Monday, 16 December 2019 at 18:01:06 UTC, Steven Schveighoffer 
wrote:
> I'd compare the typeid directly:
>
> auto tid = typeid(object);
>
> return(tid is typeid(X) || tid is typeid(Y) || tid is typeid(Z) 
> || ...)
>
> If you are doing a cast(const(X))object, what you are doing is 
> each time traversing the linked-list of typeids anyway doing 
> the same as the above, but doing extra work. So this should be 
> faster.
>
> -Steve

Great. Thanks.

Because we're only interested in non-abstract classes I adjusted 
your code to

     static foreach (Type; AliasSeq!(...))
     {
         static assert(!__traits(isAbstractClass, Type));
         if (zingid is typeid(Type)) { return true; }
     }



More information about the Digitalmars-d-learn mailing list