https://github.com/curve/eraser
✏️ A C++20 type-erasure library
https://github.com/curve/eraser
cpp cpp-library cpp20 cpp20-library polymorphism type-erasure
Last synced: 4 months ago
JSON representation
✏️ A C++20 type-erasure library
- Host: GitHub
- URL: https://github.com/curve/eraser
- Owner: Curve
- License: mit
- Created: 2024-12-25T22:28:56.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-12-29T19:09:15.000Z (about 1 year ago)
- Last Synced: 2025-05-08T18:50:07.607Z (8 months ago)
- Topics: cpp, cpp-library, cpp20, cpp20-library, polymorphism, type-erasure
- Language: C++
- Homepage:
- Size: 54.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A C++20 type-erasure library that supports interfaces
## 👋 Introduction
_Eraser_ is an easy, customizable, C++20 type-erasure library that supports user defined interfaces.
## 📦 Installation
* Using [CPM](https://github.com/cpm-cmake/CPM.cmake)
```cmake
CPMFindPackage(
NAME eraser
VERSION 2.2.1
GIT_REPOSITORY "https://github.com/Curve/eraser"
)
```
* Using FetchContent
```cmake
include(FetchContent)
FetchContent_Declare(eraser GIT_REPOSITORY "https://github.com/Curve/eraser" GIT_TAG v2.2.1)
FetchContent_MakeAvailable(eraser)
target_link_libraries( cr::eraser)
```
##
## 📖 Examples
Given two classes `erase_me` and `erase_me_too`:
https://github.com/Curve/eraser/blob/b61dc0f00541ad2ffd37e83c69b6e5d1e0b726b4/tests/classes.hpp#L5-L37
There are two ways to "erase" them:
...the normal approach:
https://github.com/Curve/eraser/blob/b61dc0f00541ad2ffd37e83c69b6e5d1e0b726b4/tests/erased.cpp#L13-L22
https://github.com/Curve/eraser/blob/b61dc0f00541ad2ffd37e83c69b6e5d1e0b726b4/tests/erased.cpp#L27-L36
... or the experimental/"hacky" approach:
https://github.com/Curve/eraser/blob/b61dc0f00541ad2ffd37e83c69b6e5d1e0b726b4/tests/deduce.cpp#L9-L20
https://github.com/Curve/eraser/blob/b61dc0f00541ad2ffd37e83c69b6e5d1e0b726b4/tests/deduce.cpp#L29-L38
---
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.
## ⭐ Credits
- [[boost-ext/te]](https://github.com/boost-ext/te): Inspiration for the `experimental` interface.