#include <isderivedfrom.h>
Classes | |
class | No |
class | Yes |
Public Types | |
enum | { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) } |
The IsDerivedFrom<D,B> class provides class hierarchy information at compile time, allowing the programmer to implement different behaviours for different class hierarchies.
The class provides two constructs. 1: IsDerivedFrom<D,B>::Is == 1 at compile time if D is or derived from B, 0 otherwise. 2: Inheriting from IsDerivedFrom<D,B> will only compile if D is or derived from B.
Example uses: 1: template<typename T, int> class Impl { ... generic implementation ... }; template<typename T, 1> class Impl { ... specialization for B and subclasses ... }; template<typename T> class Class { Impl<T, IsDerivedFrom<T, B>::Is> impl; ... delegating to impl ... };
The above example will provide the generic impl if T is not derived from B (IsDerivedFrom::Is == 0), and the specialization if T is derived from B (IsDerivedFrom::Is == 1).
2: template<typename D> class Class: IsDerivedFrom<D,B> { ... };
The above example will throw a compile error if D is not (derived from) B.