6

The following code doesn't work, as you can't static_cast from private base class.

Replacing the cast with a C-style cast works (although I originally thought this would invoke undefined behaviour apparently it does not, see this answer), but is rather ugly as it also allows you to bypass const-checking, etc. The other approach would be to make CRTPBase a friend, but that would expose all of Derived's private members.

Is there another way of writing this without using a C-style cast and without making CRTPBase a friend?

template<typename T>
struct CRTPBase {
    void callBase() {
        T * derived = static_cast<T*>(this);
        derived->publicMethod();
    }
};

struct Derived : private CRTPBase<Derived> {
    void callParent() { this->callBase(); }
    void publicMethod() {}
};

int main() {
    Derived d;
    d.callParent();
    return 0;
}
15
  • I've noticed this situation several times now, and didn't find a different solution than require public final implementations. Though a private primary base declaration of a method can be made, if you have more than one inheritance level. Apr 21, 2014 at 16:32
  • 2
    @πάνταῥεῖ: It's just way more roundabout than it should be: template <void (T::*FP)()> void callBase(T * p) { (p->*FP)(); }, call with callBase<&Derived::publicMethod>(this). Probably optimizable so that no actual arguments get passed.
    – Kerrek SB
    Apr 21, 2014 at 16:44
  • 1
    is there any reason why you don't make inheritance public and the callback protected? it seems to me the obvious choice. Apr 21, 2014 at 17:20
  • 1
    @Alex That's actually the best solution in this case, and it actually applies to my original problem also, not sure why I didn't consider that.
    – jleahy
    Apr 21, 2014 at 19:28
  • 1
    @Alex: You should definitely post that as an answer.
    – Kerrek SB
    Apr 21, 2014 at 22:18

2 Answers 2

2

I think the best solution is to avoid private inheritance and instead opt for data hiding. Marking the member function protected will prevent access from everywhere except derived classes. A further bonus public inheritance is used instead.

template<typename T>
class CRTPBase {
protected:
  void callBase() {
    T * derived = static_cast<T*>(this);
    derived->publicMethod();
  }
};

struct Derived : public CRTPBase<Derived> {
  void callParent() { this->callBase(); }
  void publicMethod() {}
};

int main() {
  Derived d;
  d.callParent();
  d.callBase() // <- illegal
  return 0;
 }
2

Not an ideal solution, but you can restrict the friendship to a unique method as follow:

template<typename T>
struct CRTPBase {
    friend T; // So T can see asDerived.
    void callBase() { asDerived()->publicMethod(); }
private:
    T* asDerived() { return static_cast<T*>(this); }
};

struct Derived : private CRTPBase<Derived> {
    friend Derived* CRTPBase<Derived>::asDerived();
    void callParent() { this->callBase(); }
    void publicMethod() {}
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.