首页 > 解决方案 > 是否有一种编译时方法来断言基类是“第一个”并且与 C++ 中的子类的偏移量为 0

问题描述

class MiClass : public Base1, public Base2 {
  
};

// assuming all base classes have a virtual destructor.
// I want a compile time test to determine if the offset of Base1 in MiClass
// is 0 at compile time so a template will fail to compile if the offset of 
// the base is NOT 0  ie: so in the compile time assert Base1 will be ok 
// but testing Base2 will fail.
// to assert that:

void *vp = xxx; ( assume a valid pointer to an MiClass )

MiClass *mip = reinterpret_cast<MiClass*>(vp);
Base1 *b1p = reinterpret_cast<Base1*>(vp);

such that static_cast<Base1*>(mip) == b1p;

是否有可以放置在静态断言中的 boost 模板,例如:

is_single_inherited_subclass_of_base<MiClass,SomeBaseInMiClass>()

?

标签: c++

解决方案


推荐阅读