test if object is instance of class at compile time

Elvis Maehren maehlvis at googlemail.com
Thu Dec 22 02:19:04 PST 2011


Jacob Carlborg wrote:
> On 2011-12-21 21:15, Elvis Maehren wrote:
>> This works fine at runtime, but explodes in CTFE:
>> ---
>> bool isInstanceOf(T, O)(O o) {
>> return (cast(T) o) !is null;
>> }
>> ---
>>
[...]
> I don't think you can do a test like that during compile time. In your
> above function, isInstanceOf, "o" will always be of the type "O". You
> can always do a comparisons to see if O is the same type as T.
> 

I don't think testing for equality of O and T gets me anywhere. T is usually 
derived from O. That makes it a different type, but still o may be castable 
to T.


More information about the Digitalmars-d-learn mailing list