Home hana types for template-of-templates
Reply: 1

hana types for template-of-templates

Hannes Ovrén
1#
Hannes Ovrén Published in 2017-12-07 11:40:02Z

I am trying to generate a type that is using template-of-templates using boost::hana but ran into trouble.

I have the following classes

template<template<typename> typename BarModel>
struct Foo {
    BarModel<double> bar;
}

template<typename T>
struct BarOne {
    T x;
}

template<typename T>
struct BarTwo {
    T y;
}

I now want to create a Foo<BarImpl> for each of the BarX<T> classes:

auto bar_types = hana::tuple_t<hana::template_t<BarOne>, hana::template_t<BarTwo>>;

hana::for_each(bar_types, [](auto t) {
    auto footype = SOMETHING(t);
});

Problem, is I am not sure how this is supposed to be done. My first attempt was to do

using BarT = typename decltype(t)::type;
auto bar_t = BarT(); // template_t, can create BarX<T> classes

auto foo_t = hana::template_<Foo>; // <-- FAIL
auto foo_bar_t = foo_t(bar_t);

but this fails with a

error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class ...> class F> constexpr const boost::hana::template_t<F> boost::hana::template_<F>’

note:   expected a template of type ‘template<class ...> class F’, got ‘template<template<class> class BarModel> class Foo’

The note suggests that hana::template_ does not work with template-of-templates. Is this the case? If so, is there an alternative solution?

Jason Rice
2#
Jason Rice Reply to 2017-12-07 22:47:17Z

Boost.Hana does not support this directly, but implementing it for this case is just a few lines of code.

Check it out:

#include <boost/hana.hpp>

namespace hana = boost::hana;

template <template <template <typename...> class> class F>
struct template_template_t
{
  template <template <typename...> class G>
  constexpr auto operator()(hana::basic_type<hana::template_t<G>>) const
    -> hana::type<F<G>>
  { return {}; }
};

template <template <template <typename...> class> class F>
constexpr auto template_template = template_template_t<F>{};

/*****/

template <template <typename...> class BarModel>
struct Foo {
  BarModel<double> bar;
};

template <typename T>
struct BarOne {
  T x;
};

template <typename T>
struct BarTwo {
  T y;
};

int main() {
  constexpr auto bar_types = hana::tuple_t<hana::template_t<BarOne>, hana::template_t<BarTwo>>;

  BOOST_HANA_CONSTANT_ASSERT(hana::equal(
    hana::transform(bar_types, template_template<Foo>)
  , hana::tuple_t<Foo<BarOne>, Foo<BarTwo>>
  ));
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321258 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO