Convert types in hana::tuple to std::vector<type> within a hana::tuple

495 views Asked by At

I've been using a lite mpl that I wrote myself, but I need it to be more robust. Currently, I am looking at using boost::hana, and it seems to have everything I need, with one exception: I don't see any way to change the types within the hana::tuple to a container of those types.

For example, this is what I was using to transform a std::tuple of types into a std::tuple of std::vectors of those types:

#include <vector>
#include <tuple>

template<typename... Ts>
struct Typelist{
};

// Declare List
template<class> class List;

// Specialize it, in order to drill down into the template parameters.
template<template<typename...Args> class t, typename ...Ts>
struct List<t<Ts...>> {
    using type = std::tuple<std::vector<Ts>...>;
};

// Sample Typelist

struct A{};
struct B{};
struct C{};

using myStructs = Typelist<A,B,C>;

// And, the tuple of vectors:

List<myStructs>::type my_tuple;

// Proof

int main()
{
    std::vector<A> &a_ref=std::get<0>(my_tuple);
    std::vector<B> &b_ref=std::get<1>(my_tuple);
    std::vector<C> &c_ref=std::get<2>(my_tuple);
    return 0;
}

Is there a boost::hana variant of this that I am overlooking? Or do I need to bring this over and change it up to work with hana::tuple instead of std::tuple?

1

There are 1 answers

0
TartanLlama On BEST ANSWER

You can use hana::transform for this:

#include <vector>
#include <boost/hana/transform.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/equal.hpp>

namespace hana = boost::hana;

struct A {};
struct B {};
struct C {};

auto types = hana::tuple_t<A,B,C>;
auto vecs = hana::transform(types, [](auto t) {
    return hana::type_c<std::vector<typename decltype(t)::type>>;
});    

auto main() -> int {
    static_assert(
        vecs == hana::tuple_t<std::vector<A>, std::vector<B>, std::vector<C>>,
        "wat"
    );
    return 0;
};

This maps over the types in the types tuple and throws them into std::vector.