C++: Can virtual inheritance be detected at compile time?

后端 未结 8 427
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 10:55

I would like to determine at compile time if a pointer to Derived can be cast from a pointer to Base without dynamic_cast<>. Is this possible using templates and metaprogramm

8条回答
  •  -上瘾入骨i
    2021-02-02 11:51

    I had the same problem, once. Unfortunately, I'm not quite sure about the virtual-problem. But: Boost has a class named is_base_of (see here) which would enable you to do smth. like the following

    BOOST_STATIC_ASSERT((boost::is_base_of::value));
    

    Furthermore, there's a class is_virtual_base_of in Boost's type_traits, maybe that's what you're looking for.

提交回复
热议问题