Skip to content

Instantly share code, notes, and snippets.

@yaqwsx
Created July 29, 2021 08:49
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 yaqwsx/68fc7d24b32d8a2d1fc64edd69744ccb to your computer and use it in GitHub Desktop.
Save yaqwsx/68fc7d24b32d8a2d1fc64edd69744ccb to your computer and use it in GitHub Desktop.
#include <iostream>
#define CRTP_SELF() \
Self& self() { return *static_cast< Self* >( this ); } \
const Self& self() const { return *static_cast< const Self* >( this ); }
template< template < typename > typename... Features >
struct Base: public Features< Base < Features... > >... {};
template < typename Self >
struct FeatureA {
CRTP_SELF();
void testA() {
std::cout << "testA invoked\n";
}
void invokeBFromA() {
std::cout << "Trying to invoke testB\n";
self().testB();
}
};
template < typename Self >
struct FeatureB {
CRTP_SELF();
void testB() {
std::cout << "testB invoked\n";
}
void invokeAFromB() {
std::cout << "Trying to invoke testA\n";
self().testA();
}
};
using MyType = Base< FeatureA, FeatureB >;
int main() {
MyType x;
x.invokeAFromB();
x.invokeBFromA();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment