Created
July 29, 2021 08:49
-
-
Save yaqwsx/68fc7d24b32d8a2d1fc64edd69744ccb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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