#include <cassert>
struct a
{
virtual ~a() {}
char a_[10];
};
struct b
{
virtual ~b() {}
char b_[20];
};
struct c : public a
{
virtual ~c() {}
char c_[15];
};
struct d : public b, a
{
virtual ~d() {}
char d_[5];
};
int main()
{
a a_;
c c_;
d d_;
a* a__ = &a_;
a* c__ = &c_;
a* d__ = &d_;
assert((void*)&a_ == (void*)a__);
assert((void*)&c_ == (void*)c__);
assert((void*)&d_ == (void*)d__); // error on most compiler
}
I'm looking for a way to test void* casting safety among class inheritance graph which can detect third assertion in compile time.
template<typename Base, typename Derived>
struct test
{
enum {
is_safe = (static_cast<Derived*>(static_cast<Base*>(nullptr)) == nullptr)
};
};
My intention is described in above code, but it won't be compiled because casting is not constant expression. Is it possible to check it in platform/compiler independent way?