Is there a way to prevent a class from being derived from twice using a static assert and type trait?

前端 未结 3 1818
北海茫月
北海茫月 2021-01-19 08:10

I realize this is a contrived example, but I want a compile check to prevent this...

class A {};
class B : public A {};
class C : public A {};

class D : pub         


        
3条回答
  •  无人及你
    2021-01-19 08:57

    The following should work:

    BOOST_STATIC_ASSERT(((A*)(D*)0 == 0)) 
    

    If A exists twice, this should rise an ambiguity error, while otherwise the test will always succeed (because it compares two null pointers).

提交回复
热议问题