samedi 5 mai 2018

Check if child class performed a method override

I want to know if a child class did an override of a virtual method, in this case I want to insert the instance in a special list for additional processing. What I have until now using CRTP:

template<class T>
class P {
public:
    P() {
       if (!std::is_same<decltype(&T::foo),decltype(&P::foo)>::value)
         .........do something.........
    }
    virtual ~P(){}
    virtual void foo() {}
}

class Child: public P<Child> {
public:
    Child() {
    }
    virtual void foo() {
    }
}

I want to know if there's some tricks to avoid the CRTP in this case, because currently P class is not a template and I should change several lines of codes. I could but I want to know if there are alternatives. I tried to use the same schema but just in another function, not in the P constructor, but I should iterate over any single child of P (I have several child classes). Is it possible to "iterate over types"? I have a list of P pointers, but obviously a runtime check is deeply different from a static check like std::is_same. I tried to use:

(void*)(obj.*(&P::foo)) != (void*)(&P::foo)

and it actually works with gcc 4.8.1 but I should disable the warnings using -Wno-pmf-conversions and it doesn't seem the best choice.

Aucun commentaire:

Enregistrer un commentaire