Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble verifying that my super class or base class is what a macro or template claims it to be
    primarykey
    data
    text
    <p>I've been dealing with a frustrating problem recently. I have a lot of reflection code I'd like to hide in macros. These <code>COMPONENT_x()</code> macros are used to like so:</p> <pre><code>class ComponentBase : public IComponent { COMPONENT(ComponentBase) }; class ComponentDerived1 : public ComponentBase { COMPONENT_DERIVED(ComponentDerived1, ComponentBase) }; </code></pre> <p>The above is completely valid. However, I would like a compile error to occur in this case:</p> <pre><code>class ComponentDerived2 : public ComponentDerived1 { COMPONENT_DERIVED(ComponentDerived2, ComponentBase) // ^^^^^^^^^^^^^ // This type claims to have a superclass of ComponentBase in the above macro, // but we really derive from ComponentDerived1. // // I want this to result in a compile error. }; </code></pre> <p>That is, when I'm claiming that my super class (or my base class) is <code>ComponentBase</code> when my the super class is actually <code>ComponentDerived1</code>, I would ideally like a compile-time error.</p> <p>The reason I cannot easily detect this case is that while my super class is <code>ComponentDerived1</code>, that class' super is <code>ComponentBase</code> — and thus it is also one of my base classes. (I understand that <code>ComponentDerived1</code> is-a <code>ComponentBase</code>, so maybe there is a better phrase to use than "base class.")</p> <p>There were some comments questioning why I want to do this. I'm using an optimized system of component generation which puts all objects of the same type into discrete buffers, so generating knowledge of class relationships is important if I want to have an API such as <code>getComponentsThatImplement(ComponentDerived1::getType());</code>.</p> <p>I had hacked together <a href="http://codepad.org/zHEdiMDQ" rel="nofollow">one solution</a> that works with g++:</p> <pre><code>class ComponentBase : public IComponent { COMPONENT(ComponentBase) protected: static void helperComponentBase(); // COMPONENT(ComponentBase) }; class ComponentDerived1 : public ComponentBase { COMPONENT_DERIVED(ComponentDerived1, ComponentBase) private: using ComponentBase::helperComponentBase; // COMPONENT_DERIVED(..., ComponentBase) }; class ComponentDerived2 : public ComponentDerived1 { COMPONENT_DERIVED(ComponentDerived2, ComponentBase) private: using ComponentBase::helperComponentBase; // error: this function is already hidden } </code></pre> <p>Unfortunately, clang with Xcode 4.3.2 seems to not support <code>using</code> in this way. I've check boost's type traits library and don't see anything useful. I am using C++11, so I can use modern constructs.</p> <p>Any other clever ideas?</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.
 

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