- No macros
- Concise syntax for defining and using mixins
- Supports allocators
constexprin C++26
#include <simply/destructible.hpp>
#include <simply/dyn.hpp>
#include <iostream>
#include <numbers>
#include <vector>
struct insertable : simply::member_base {
static auto fn(const auto &value, std::ostream &out) -> std::ostream & {
return out << value;
}
};
template <typename Self>
struct simply::iface<insertable, Self> {
friend auto operator<<(std::ostream &out, const Self &self) -> std::ostream & {
return simply::fn<insertable>(self, out);
}
};
int main() {
using namespace std::string_literals;
struct mixins : simply::composes<insertable, simply::destructible> {};
std::vector<simply::dyn<mixins>> values;
values.emplace_back("Hello, world!"s);
values.emplace_back(4);
values.emplace_back(std::numbers::pi);
for (const auto &value : values) {
std::cout << value << '\n';
}
}- Clang 19, 20
- GCC 14, 15
dyn<Mixin, Storage, Dispatch>for type erasure of mixins- Concepts:
mixin<Mixin>fundamental<Mixin>compound<Mixin>composition<Mixin>choice<Mixin>mixin_for<Mixin, T>has_mixin<T, Mixin>
- Predefined mixins for common use-cases:
composes<Mixins...>chooses<Mixins...>destructiblemovablecopyableinvocable<Fn>extractable<In>insertable<Out>identifiableallocator_storage<Allocator>indirect_dispatchinplace_dispatch
iface<Mixin, Self>to specialize interfaces ofdynimpl<Mixin, T>to specialize implementations of a mixin
inplace_storage<Size, Align>shared_storagecopy_on_write_storagenullableretrievabletrivially_copyabletrivially_destructibleequality_comparable<Policy>three_way_comparable<Ordering, Policy>- Policy determines behavior when erased types mismatch
assignable_from<T>support for type erasing sinksslot_dispatchfor vtable lookup using a static slot map key- Mixins for iterators
- Specializations of
implto delegate member mixins through:std::reference_wrapperstd::unique_ptrstd::shared_ptrstd::weak_ptrstd::indirectstd::polymorphicstd::variant(if every alternative has the mixin)std::optional(if bothTandstd::nullopt_thave the mixin)std::expected(if bothTandEhave the mixin)std::pair(if eitherfirstorsecondexclusively has the mixin)std::tuple(if one element exclusively has the mixin)
- Specializations of
dynthat behave equivalently to:std::functionstd::copyable_functionstd::move_only_functionstd::function_refstd::anystd::ranges::any_view