用于模板的HANA类型
hana types for template-of-templates
我正在尝试生成一种使用boost :: hana但遇到麻烦的类型。
我有以下类
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>
:
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>>
));
}
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 增强 Hana index_if和类型
- Boost.Hana:在 constexpr 上下文中将值元组转换为相应类型的元组
- 从具有部分专用化的boost:hana::set中提取类型失败
- 是否可以使用 Boost.Hana 将 std::array 解压缩到非类型模板参数包中
- 使用 hana::transform 在 C++14 中转换元组内的类型
- 为什么BOOST :: HANA :: EXAREMIS ::键入实验功能?类型列表的棘手是什么
- 用于模板的HANA类型
- 使用 boost::hana 获取函数参数的类型
- Boost.Hana:如何检查函数是否具有特定类型的专用性
- 如何根据表达式在Boost.Hana中是否有效来过滤类型元组
- 增强元组类型的hana大小
- 使用boost::hana过滤类型列表
- 创建具有不可移动/不可复制类型的hana元组
- 在集合中提升hana查找类型
- 使用增强 hana 检查特定的嵌套类型/标记