检查元组类型是否是彼此的子集

时间:2016-01-07 10:12:24

标签: c++ boost-mpl boost-hana

假设我有2个未实例化的元组。有没有惯用的方法来检查一组是否是另一组的子集?

如果这需要其他类型而不是hana::tuple_c,那么这也很好。实际上,我当前的输入包含std::tuple,但我无法以任何方式使用它。

NOT 工作的代码(但我觉得应该有类似的东西):

#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, "");

1 个答案:

答案 0 :(得分:9)

您没有正确使用boost::hana。这将有效:

#include <boost/hana.hpp>
using namespace boost;

constexpr auto setA = hana::tuple_t<int, char, float>;
constexpr auto setB = hana::tuple_t<int, float>;

// Is `setB` a subset of `setA`? (Yes.)
static_assert(hana::is_subset(setB, setA), "");

// Is `setA` a subset of `setB`? (No.)
static_assert(!hana::is_subset(setA, setB), "");

说明:

hana::tuple_t<xs...>hana::type_c个元组的简写符号。

  • auto x = hana::tuple_t<int, char>;
    // ...is equivalent to...
    auto x = hana::make_tuple(hana::type_c<int>, hana::type_c<char>);
    

hana::type_c个对象将类型换行为值。

hana::is_subset(a, b)检查如果ab 的子集,而不是反对(您正在检查b是否为a您问题中include_once "codeigniter/index.php/user"; 的子集