【发布时间】:2016-04-11 16:24:37
【问题描述】:
假设我有 2 个未实例化的元组。有没有一种惯用的方法来检查一个集合是否是另一个集合的子集?
如果这需要其他类型而不是hana::tuple_c,这也可以。实际上,我当前的输入由std::tuple 组成,但我无法让它工作。
不工作的代码(但我觉得应该有类似的可能):
#include <boost/hana.hpp>
using namespace boost;
using SetA = hana::tuple_c<int, char, float>;
using SetB = hana::tuple_c<int, float>;
static_assert(
hana::is_subset( SetB, SetA ),
""
);
我当前的解决方法是使用boost::mpl 进行交集,然后比较结果。这可行,但我对纯 boost::hana 解决方案感兴趣:
#include <boost/mpl.hpp>
using namespace boost;
using SetA = mpl::set<int, char, float>;
using SetB = mpl::set<int, float>;
using Intersection = typename mpl::copy_if<
SetA,
mpl::has_key< SetB, mpl::_1 >,
mpl::back_inserter< mpl::vector<> >
>::type;
// since Intersection is a vector, subset also needs vector type
using Subset = typename mpl::copy<
SetB,
mpl::back_inserter< mpl::vector<> >
>::type;
static_assert(std::is_same<Intersection, Subset>::value, "");
【问题讨论】:
标签: c++ boost-mpl boost-hana