Skip to content

Instantly share code, notes, and snippets.

@dirvine
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirvine/e3cd176cc21cd581698f to your computer and use it in GitHub Desktop.
Save dirvine/e3cd176cc21cd581698f to your computer and use it in GitHub Desktop.
example inherit/crtp forwarding ctr for derived from base
#include <iostream>
#include <string>
using namespace std;
template <typename Child>
struct Routing {
Routing() = delete;
explicit Routing(std::string s) : t(s) {}
void Get() { static_cast<Child*>(this)->FacadeGet(); }
void Hello() { std::cerr << t; }
string t;
};
template <typename Child>
struct MaidMgr {
void interface() {
static_cast<Child*>(this)->FacadeGet();
CallHello();
}
void CallHello() { static_cast<Child*>(this)->Hello(); }
void Get() {
std::cerr << "MaidMgr Get\n";
static_cast<Child*>(this)->Hello();
}
};
template <typename Child>
struct DataMgr {
void interface() {
static_cast<Child*>(this)->FacadeGet();
CallHello();
}
void CallHello() { static_cast<Child*>(this)->Hello(); }
};
struct Facade : public MaidMgr<Facade>, public DataMgr<Facade>, public Routing<Facade> {
Facade() = delete;
Facade(std::string s) : Routing(s) {} // forwarding ctr - unsure msvc will allow this
void FacadeGet() { cerr << "Derived implementation " << x << "\n"; }
string x = "test";
};
int main() {
Facade d(std::string("hello"));
d.template MaidMgr<Facade>::Get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment