从带有部分专业化的boost:hana :: set提取类型失败

时间:2018-07-26 05:14:50

标签: c++ templates boost boost-hana

我正在使用以下模板声明一组类型:

template<class ...T>
using DependencySet = boost::hana::set<boost::hana::type<T>...>;

我希望能够从集合中提取这些类型并放入另一个容器中。我尝试使用“经典”方法:

template<class ...T>
struct ExtractTypes;

template<class ...Dependencies>
struct ExtractTypes<DependencySet<Dependencies...>>
{
    using type = SomeOtherType<Dependencies...>;
};

A,编译器不同意:

  错误:类模板的部分专业化包含一个模板   无法推论的参数;这种部分专业化将   永远不会使用[-Wunusable-partial-specialization]

     

struct ExtractTypes >

有没有办法从这样的集合中提取类型?

1 个答案:

答案 0 :(得分:2)

关于编译器错误,我认为这是不正确的,并且您可能正在使用旧版本的Clang或GCC。

即使使用最新的编译器,您的代码也不正确,因为它假设hana::set的模板参数被记录为定义的实现,因为set的含义是成为无序的关联容器。

考虑使用“类型即值”方法,该方法允许更具表现力的代码,而Boost.Hana旨在帮助实现这一目的。

要进行设置,请使用hana::make_set,然后使用hana::unpack来获取值,可以使用mp_apply来调用可变参数函数对象及其包含的值(无特定顺序)。

这里是一个例子:

#include <boost/hana.hpp>
#include <type_traits>

namespace hana = boost::hana;

template <typename ...T>
struct DependencySet { };

int main()
{
  auto deps = hana::make_set(
    hana::type<char>{}
  , hana::type<int>{}
  , hana::type<long>{}
  );

  auto dep_types = hana::unpack(deps, hana::template_<DependencySet>);

  static_assert(
    hana::typeid_(dep_types) == hana::type<DependencySet<char, int, long>>{}
  , ""
  );

}

顺便说一句,如果您只想将模板参数从一个模板放到另一个模板中,请看一下Boost.Mp11的{{3}}