https://github.com/briancairl/crtp
Simple CRTP macro utility library
https://github.com/briancairl/crtp
Last synced: 2 months ago
JSON representation
Simple CRTP macro utility library
- Host: GitHub
- URL: https://github.com/briancairl/crtp
- Owner: briancairl
- Created: 2019-11-12T03:02:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-12T03:03:09.000Z (over 5 years ago)
- Last Synced: 2025-02-13T04:31:11.588Z (4 months ago)
- Language: C++
- Size: 2.93 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CRTP
This is a ridiculously simple library that contains a few macros.
I end up copying them into every new library I made, anyway, so I figured I would just make one library for the sake of consistency.
Consider the following example use case, which creates class templates which follow the chain
Base -> Derived1 -> Derived2
```c++
/**
* @brief Base
*/
template
class TestInterface
{
public:
inline void do_thing() const
{
CRTP_INDIRECT_M(do_wrapped_thing)();
}constexpr int do_other_thing(int i)
{
return CRTP_INDIRECT_M(do_other_thing)(i);
}private:
IMPLEMENT_CRTP_BASE_CLASS(TestInterface, DerivedT);
};/**
* @brief Base -> Derived1
*/
template
class TestWrappedInterface : public TestInterface>
{
private:
void CRTP_OVERRIDE_M(do_wrapped_thing)() const
{
CRTP_INDIRECT_M(do_wrapped_thing)();
}constexpr int CRTP_OVERRIDE_M(do_other_thing)(int i)
{
return i + i + i;
}IMPLEMENT_CRTP_BASE_CLASS(TestWrappedInterface, DerivedT);
IMPLEMENT_CRTP_DERIVED_CLASS(TestInterface, TestWrappedInterface);
};/**
* @brief Base -> Derived1 -> Derived2
*/
class TestDerived : public TestWrappedInterface
{
public:
TestDerived() : TestWrappedInterfaceType{} {};private:
void CRTP_OVERRIDE_M(do_wrapped_thing)() const
{
// does nothing, but compiles
}IMPLEMENT_CRTP_DERIVED_CLASS(TestWrappedInterface, TestDerived);
};
```