Skip to content

bluescarni/tanuki

Repository files navigation

tanuki

Build Status Build Status language Code Coverage


A type-erasure toolkit for C 20
Explore the docs »

Report bug · Request feature · Discuss

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:

  • high configurability,
  • support for both value and reference semantics,
  • small object optimisation to avoid dynamic memory allocation,
  • the ability to type-erase references,
  • support for composite interfaces,
  • optional support for Boost.serialization.

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.

The elevator pitch

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!

Documentation

The full documentation can be found here.