首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板的hana类型

模板的hana类型
EN

Stack Overflow用户
提问于 2017-12-07 19:40:03
回答 1查看 162关注 0票数 3

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

我有以下课程

代码语言:javascript
复制
template<template<typename> typename BarModel>
struct Foo {
    BarModel<double> bar;
}

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

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

现在,我想为每个BarX<T>类创建一个Foo<BarImpl>

代码语言:javascript
复制
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);
});

问题是,我不确定这应该怎么做。我的第一个尝试是

代码语言:javascript
复制
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);

但是这失败了,因为

代码语言:javascript
复制
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_不能与模板的模板一起工作。真的是这样吗?如果是这样的话,有没有其他解决方案?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-08 06:47:18

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

看看这个:

代码语言:javascript
复制
#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>>
  ));
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47694300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档