A type-erasure toolkit for C++20
MPL-2.0 License
tanukis are a kind of supernatural beings found in the classics and in the folklore and legends of various places in Japan. They are reputed to be mischievous and jolly, masters of disguise and shapeshifting but somewhat gullible and absent-minded.
tanuki is a small, single-header and self-contained C++20/23 toolkit for type-erasure. Main features include:
Interfaces are defined and implemented blending the familiar language of traditional object-oriented programming (i.e., abstract base clasess, default implementations, single/multiple inheritance, etc.) with C++20 concepts.
Here is a minimal approximation of std::any in tanuki:
#include <string>
#include <tanuki/tanuki.hpp>
// Interface implementation.
template <typename Base, typename Holder, typename T>
struct any_iface_impl : public Base {
};
// Interface.
struct any_iface {
template <typename Base, typename Holder, typename T>
using impl = any_iface_impl<Base, Holder, T>;
};
int main()
{
using any_wrap = tanuki::wrap<any_iface>;
// Store an integer.
any_wrap w1(42);
// Store a string.
any_wrap w2(std::string("hello world"));
// Store anything...
struct foo {
};
any_wrap w3(foo{});
}
Try it on compiler explorer!
The full documentation can be found here.