test if object is instance of class at compile time

Elvis Maehren maehlvis at googlemail.com
Wed Dec 21 12:15:34 PST 2011


This works fine at runtime, but explodes in CTFE:
---
bool isInstanceOf(T, O)(O o) {
	return (cast(T) o) !is null;
}
---

CTFE doesn't like "!is null" ("cannot compare [...] at compile time"). 

Moreover, in CTFE a failed cast terminates the interpretation ("cannot 
reinterpret class from [...] to [...] at compile time"). Is this somehow 
catchable? If so, I could do something along these lines:
---
if(__ctfe) {
	try {
		auto t = cast(T) o;
		return true;
	} catch {
		return false;
	}
}
---



More information about the Digitalmars-d-learn mailing list