Skip to content

Instantly share code, notes, and snippets.

@caryhaynie
Last active October 1, 2016 08:05
Show Gist options
  • Select an option

  • Save caryhaynie/3099259 to your computer and use it in GitHub Desktop.

Select an option

Save caryhaynie/3099259 to your computer and use it in GitHub Desktop.
Simple CRTP pattern using friend classes to hide implementing methods.
#include <iostream>
template <typename T>
class TestBase {
public:
void TestIface() {
static_cast<T*>(this)->TestImpl();
}
};
#define IMPLEMENTS(impl, iface) friend class iface<impl>
class Test : public TestBase<Test> {
IMPLEMENTS(Test, TestBase);
private:
void TestImpl() {
std::cout << "In TestImpl()" << std::endl;
}
};
template <typename T>
void func(TestBase<T>* iface) {
iface->TestIface();
}
int main(int argc, char** argv) {
Test t;
t.TestIface();
func(&t);
return 0;
}
@pavelschon
Copy link

I have better solution w/o macro. See my gist: https://gist.github.com/pavelschon/67d987aaf173dfcc08279a132fccb9d8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment