[英]Check if tuple types are subsets of each other
假設我有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, "");
您沒有正確使用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
對象元組的簡寫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)
檢查是否a
的一個子集b
,而不是反之亦然(你檢查,如果b
是一個子集, a
在你的問題)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.