C++Guns – RoboBlog

27.10.2017

C++ Guns - multi derived CRTP

Filed under: Allgemein — Tags: — Thomas @ 19:10

As we all know, CRTP is bad. The reasons against you can read on my unpublish Artikel "Why CRTP is a bad choise".

Nevermind. Here come a CRTP with a linear derived chain.

#include <iostream>

using namespace std;

template<typename Child>
struct Base : public Child {
  void func() {
    cout << "Base\n";
    static_cast<Child*>(this)->func();
  }
};

template<typename Child>
struct Base2 : public Child {
  void func() {
    cout << "Base2\n";
    static_cast<Child*>(this)->func();
  }
};

struct Child {
   void func() {
     cout << "Child\n";
   }
};


int main() {
  Base<Base2<Child>> base;
  base.func();
}
Base
Base2
Child

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress