Check if tuple types are subsets of each other

Let's say I have 2 tuples that are not created. Is there an idiomatic way to check if one set is a subset of another?

If this requires a different type instead of hana::tuple_c , this is also good. In fact, my current input consists of std::tuple , but I could not get it to work anyway.

Code that does NOT work (but I feel there should be something similar):

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

In my current workaround, boost::mpl is used to perform the intersection and then compare the results. This works, but I'm interested in a clean boost::hana solution:

 #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, ""); 
+5
source share
1 answer

You are not using boost::hana correctly. This will work:

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

Explanation:

hana::tuple_t<xs...> is an abbreviated note for a tuple of 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 objects wrap types in values.

hana::is_subset(a, b) checks if a is a subset of b and not vice versa (you checked if b subset of a in your question).

+9
source

Source: https://habr.com/ru/post/1240073/


All Articles