r/cpp•
braxtons12

Introducing hyperion::mpl, C++20 metaprogramming library

(No?) Thanks to layoffs, over the past few weeks I've had a lot of time to break up, clean up, and otherwise improve on a long-term project of mine that I had to put on the backburner for the past year. This is one of the first results of that.

https://github.com/braxtons12/hyperion_mpl

I'll try to avoid repeating the README too much here. hyperion::mpl is a C++20 metaprogramming library focused on a value-oriented approach. It tries to make metaprogramming (comparatively) simple and easy, and much more like writing "normal" C++ code than the traditional (often recursive) class template based approaches.

Example:

#include <hyperion/mpl.h>

using hyperion::mpl::MetaType;
using hyperion::mpl::List;
using hyperion::mpl::Pair;
using hyperion::mpl::is_const;

constexpr auto as_const = [](MetaType auto type) {
    return type.as_const();
};

constexpr auto zipped = List<int, double, float>{}
                    .zip(List<size_t, void*, ptrdiff_t>{});
static_assert(zipped
              == List<Pair<int, size_t>,
                      Pair<double, void*>,
                      Pair<float, ptrdiff_t>>{});
static_assert(zipped.apply(as_const)
                    .all_of(is_const));

I know the immediate question you have is "What differentiates this or is better about it vs something like Boost.Hana?"

I think the main selling point of this vs Boost.Hana boils down to the fact that this is simpler. Boost.Hana tries to blur the lines between compile-time and run-time programming, whereas this is focused solely on making the compile-time side of things easier. This is a tradeoff: with Boost.Hana you can do things like the below (pulled from Boost.Hana's README), while with hyperion::mpl, you can't:

auto animals = hana::make_tuple(Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"});

auto names = hana::transform(animals, [](auto a) { return a.name; }); assert(hana::reverse(names) == hana::make_tuple("Snoopy", "Garfield", "Nemo"));

static_assert(hana::length(animals) == 3u);

However, there is a lot of machinery in Boost.Hana to make functionality like that possible, which which can often mean longer compiler times, and, when used incorrectly, can lead to verbose, hard to understand compiler errors. The implementation of hyperion::mpl is much simpler because it sticks strictly to improving compile-time use-cases, and thanks to that simpler implementation, along with usage of C++20 concepts, any compiler errors you do get are generally friendlier as well.

Currently tested/supported compilers/configurations:

Linux: GCC 13, Clang 16
MacOS: Clang 15, Clang 16
Windows: MSVC 19.39

Posted Feb 29, 2024, 8:27 PM UTC

3312

Comments aren't archived yet

This post had 12 comments on Reddit. Comments for this period haven't been loaded into the archive yet.