According to a simple, intrusive scoring system, I have a template<typename T> class Handle that needs to be created with a subclass of CountedBase . Handle<T> contains a pointer to T , and its destructor calls DecRef (defined in CountedBase ) on that pointer.
This can usually cause problems when trying to limit header dependencies with forward declarations:
#include "Handle.h" class Foo;
As a solution, I rewrote Handle<T>::~Handle() as follows:
template<typename T> Handle<T>::~Handle() { reinterpret_cast<CountedBase*>(m_ptr)->DecRef(); }
Note that I use reinterpret_cast here instead of static_cast , since reinterpret_cast does not require the definition of T be complete. Of course, he will also not perform pointer adjustment for me ... but for now, I am careful with layouts ( T should have CountedBase as its left ancestor, should not inherit from it practically, and for a couple of unusual platforms, additional vtable magic is required), this safely.
Which would be really nice, if only if I could get an additional level of static_cast security, where possible. In practice, the definition of T usually ends at the point where Handle::~Handle is created, making the perfect moment to double check that T really inheriting from CountedBase . If it's incomplete, I can't do much ... but if it completes, a health check will be good.
Which brings us finally to my question: Is there a way to perform a compile-time check that T inherits from CountedBase that will not cause a (false) error when T not completed?
[Usually disclaimer: I know that there are potentially dangerous and / or UB aspects for using incomplete types in this way. However, after a large number of cross-platform tests and profiling, I decided that this is the most practical approach, given some unique aspects of my use. I'm interested in compile-time checking, not a general overview of the code.]