我正在使用以下模板来声明一组类型:
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...>;
};
唉,编译器不同意:
error: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
struct ExtractTypes< DependencySet< Dependencies...>>
有没有办法从这样的集合中提取类型?
最佳答案
关于编译器错误,我认为这是不正确的,您可能使用的是旧版本的 Clang 或 GCC。
即使使用最新的编译器,您的代码也是不正确的,因为它假设了 hana::set
的模板参数。这被记录为实现定义,因为set
是一个无序的关联容器。
考虑使用“类型作为值”方法,它允许更具表现力的代码,而这正是 Boost.Hana 旨在 boost 的。
要制作一组,请使用 hana::make_set
, 要取回值,您可以使用 hana::unpack
它调用可变参数函数对象及其包含的值(无特定顺序)。
这是一个例子:
#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 的 mp_apply
关于c++ - 使用部分特化从 boost:hana::set 中提取类型失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51531444/