A C++20 type-erasure library that supports interfaces
Eraser is an easy, customizable, C++20 type-erasure library that supports user defined interfaces.
-
Using CPM
CPMFindPackage( NAME eraser VERSION 2.2.1 GIT_REPOSITORY "https://github.com/Curve/eraser" )
-
Using FetchContent
include(FetchContent) FetchContent_Declare(eraser GIT_REPOSITORY "https://github.com/Curve/eraser" GIT_TAG v2.2.1) FetchContent_MakeAvailable(eraser) target_link_libraries(<target> cr::eraser)
Given two classes erase_me and erase_me_too:
Lines 5 to 37 in b61dc0f
There are two ways to "erase" them:
...the normal approach:
Lines 13 to 22 in b61dc0f
Lines 27 to 36 in b61dc0f
... or the experimental/"hacky" approach:
Lines 9 to 20 in b61dc0f
Lines 29 to 38 in b61dc0f
While the "hacky" approach might look more pleasing, you should prefer to use the normal approach. As the name implies, the hacky approach relies on friend injection which allows one to do stateful meta-programming, which is technically not legal C++, however, all three major compilers support it.
- [boost-ext/te]: Inspiration for the
experimentalinterface.