Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can definitely use type_traits for this. Throw in a compile-time assert and you'll have it. Here's some code that should work:</p> <pre><code>#define PARENTCHECKER_THING(__TYPE,__BASE) \ inline void WastedFunc() \ { COMPILE_ASSERT( (std::is_base_of&lt;__BASE,__TYPE&gt;().value == true) ); } #define COMPONENT(__TYPE) \ bool __TYPE##SomeFunc(bool val) { return val; } \ bool __TYPE##SomeOtherThing(bool val) { return val; } #define COMPONENT_DERIVED(__TYPE,__BASE) \ bool __TYPE##SomeFunc( bool val) { return val; } \ bool __TYPE##SomeOtherThing(bool val) { return val; } \ PARENTCHECKER_THING(__TYPE,__BASE); class Base { COMPONENT(Base); int baseData; }; class NOT_Base { COMPONENT(NOT_Base); int garbage; }; class Child : public Base { protected: COMPONENT_DERIVED(Child,NOT_Base); float childData; }; </code></pre> <p>Admittedly, the base checker only needs to some base class and not the <em>immediate</em> base. I suppose I could have also been a little more clever about not adding a bogus function in there. It should be dead-stripped though.</p> <p>Hope this helps!</p> <p>EDIT: Hmmm...I missed the sentance in the middle where you called this out as a problem. How about this then:</p> <pre><code>#define PARENTCHECKER_THING(__TYPE,__BASE) \ inline void WastedFunc() \ { COMPILE_ASSERT( (std::is_same&lt; std::common_type&lt;__BASE,__TYPE&gt;()::type, __BASE &gt;().value == true) ); } </code></pre> <p>Essentially, "Assert if the common type of the base and the child is something other than the base". Unfortunately, I don't have a copy of VS2010 handy, and VS2008 doesn't support common_type&lt;>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload