c++ - 模板模板的 hana 类型

标签 c++ templates boost-hana

我正在尝试使用 boost::hana 生成一个使用模板中的模板的类型,但遇到了麻烦。

我有以下类(class)

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

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

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

我现在想创建一个 Foo<BarImpl>对于每个 BarX<T>类:

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);
});

问题是我不确定应该如何完成。 我的第一次尝试是做

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);

但这失败了

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’

注释表明 hana::template_不适用于模板模板。 是这样吗?如果是这样,是否有替代解决方案?

最佳答案

Boost.Hana 不直接支持这一点,但在这种情况下实现它只是几行代码。

检查一下:

#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>>
  ));
}

关于c++ - 模板模板的 hana 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694300/

相关文章:

c++ - Eclipse Juno Indexer Broken - 找不到任何 STL header

c++ - 错误 C2062 : type 'void' unexpected in signal declaration in QT

c++ - 使用 map 打印生日,最终结果为八进制

c++ - 在模板参数列表中跳过较早的隐式模板参数,但不会较晚

c++ - Linux 中的嵌套 vector

c++ - 使用 boost::hana 过滤具有类型的列表

c++ - 使用空函数调用 hana::is_valid 的用途是什么?

c++ - 未在命名空间中声明和定义的命名空间的类的 friend

c++ - 防止非常量左值解析为右值引用而不是 const 左值引用

c++ - 如何从参数包中定义值类型的元组