检查一组类型是否是另一组的子集

时间:2017-03-03 14:06:20

标签: c++ c++11 templates variadic-templates template-meta-programming

如何检查一个参数包(解释为一组)是否是另一个参数包的子集?

到目前为止,我只有框架(使用std :: tuple),但没有功能。

#include <tuple>
#include <type_traits>

template <typename, typename>
struct is_subset_of : std::false_type
{
};

template <typename ... Types1, typename ... Types2>
struct is_subset_of<std::tuple<Types1...>, std::tuple<Types2...>>
    : std::true_type
{
    // Should only be true_type if Types1 is a subset of Types2
};

int main() {
    using t1 = std::tuple<int, double>;
    using t2 = std::tuple<double, int>;
    using t3 = std::tuple<int, double, char>;

    static_assert(is_subset_of<t1, t1>::value, "err");
    static_assert(is_subset_of<t1, t2>::value, "err");
    static_assert(is_subset_of<t2, t1>::value, "err");
    static_assert(is_subset_of<t2, t3>::value, "err");
    static_assert(!is_subset_of<t3, t2>::value, "err");
}

每个类型不允许在一个集合中出现多次。

如果解决方案适用于C ++ 11,那就太好了。

9 个答案:

答案 0 :(得分:22)

#include <tuple>
#include <type_traits>

template <typename T, typename... Ts>
constexpr bool contains = (std::is_same<T, Ts>{} || ...);

template <typename Subset, typename Set>
constexpr bool is_subset_of = false;

template <typename... Ts, typename... Us>
constexpr bool is_subset_of<std::tuple<Ts...>, std::tuple<Us...>>
           = (contains<Ts, Us...> && ...);

DEMO

答案 1 :(得分:7)

如果您可以使用C ++ 17功能,我强烈建议您使用Piotr Skotnicki's solution

我不得不在不久前实现此功能。我只是要复制粘贴我在那时提出的代码。

我并不是说这是实施此类支票的最佳或最优雅的方式!我没有太多考虑边缘情况;您可能需要调整代码以满足您的要求。

澄清:ContainsTypes<Lhs, Rhs>检查RhsLhs的子集。

  template <typename Tuple, typename T>
  struct ContainsType;

  template <typename T, typename U, typename... Ts>
  struct ContainsType<std::tuple<T, Ts...>, U>
  {
      static const bool VALUE = ContainsType<std::tuple<Ts...>, U>::VALUE;
  };

  template <typename T, typename... Ts>
  struct ContainsType<std::tuple<T, Ts...>, T>
  {
      static const bool VALUE = true;
  };

  template <typename T>
  struct ContainsType<std::tuple<>, T>
  {
      static const bool VALUE = false;
  };

  // -----

  template <typename Lhs, typename Rhs>
  struct ContainsTypes;

  template <typename Tuple, typename T, typename... Ts>
  struct ContainsTypes<Tuple, std::tuple<T, Ts...>>
  {
      static const bool VALUE = ContainsType<Tuple, T>::VALUE && ContainsTypes<Tuple, std::tuple<Ts...>>::VALUE;
  };

  template <typename Tuple>
  struct ContainsTypes<Tuple, std::tuple<>>
  {
      static const bool VALUE = true;
  };

答案 2 :(得分:4)

这是一个C + +答案,我认为它比Piotr's answer简单得多:

template <class T, class... U>
struct contains : std::disjunction<std::is_same<T, U>...>{};

template <typename...>
struct is_subset_of : std::false_type{};

template <typename... Types1, typename ... Types2>
struct is_subset_of<std::tuple<Types1...>, std::tuple<Types2...>> : std::conjunction<contains<Types1, Types2...>...> {};

Demo

disjunctionconjunction是C ++ 17中引入的新类型特征。我们可以利用这些来检查第二个元组中的至少一个类型是否匹配第一个元组中的“下一个类型”,我们广泛使用参数包扩展。

答案 3 :(得分:3)

你可以使用类似下面的类来做到这一点:

template<typename... Set>
struct Check {
    template<typename Type>
    static constexpr bool verify() {
        using accumulator_type = bool[];
        bool check = false;
        accumulator_type accumulator = { (check = check || std::is_same<Type, Set>())... };
        (void)accumulator;
        return check;
    }

    template<typename... SubSet>
    static constexpr bool contain() {
        using accumulator_type = bool[];
        bool check = true;
        accumulator_type accumulator = { (check = check && verify<SubSet>())... };
        (void)accumulator;
        return check;
    }
};

在基于功能的示例中将其转换为简单明了 它遵循适用于您的代码的可能实现:

#include <tuple>
#include <type_traits>

template<typename... Set>
struct Check {
    template<typename Type>
    static constexpr bool verify() {
        using accumulator_type = bool[];
        bool check = false;
        accumulator_type accumulator = { (check = check || std::is_same<Type, Set>())... };
        (void)accumulator;
        return check;
    }

    template<typename... SubSet>
    static constexpr bool contain() {
        using accumulator_type = bool[];
        bool check = true;
        accumulator_type accumulator = { (check = check && verify<SubSet>())... };
        (void)accumulator;
        return check;
    }
};


template <typename, typename>
struct is_subset_of;

template <typename ... Types1, typename ... Types2>
struct is_subset_of<std::tuple<Types1...>, std::tuple<Types2...>> {
    static constexpr bool value = Check<Types2...>::template contain<Types1...>();
};

int main() {
    using t1 = std::tuple<int, double>;
    using t2 = std::tuple<double, int>;
    using t3 = std::tuple<int, double, char>;

    static_assert(is_subset_of<t1, t1>::value, "err");
    static_assert(is_subset_of<t1, t2>::value, "err");
    static_assert(is_subset_of<t2, t1>::value, "err");
    static_assert(is_subset_of<t2, t3>::value, "err");
    static_assert(!is_subset_of<t3, t2>::value, "err");
}

工作在课程Check内进行,方法为containverify
contain成员函数是入口点。它使用一个常见的技巧(在等待fold表达式时)来解压缩子集,并需要对每个包含的类型进行显式检查。成员函数verify通过将单个类型与给定集匹配来完成剩下的工作。

如果我能为您提供更多详细信息,或者它已经足够清晰,请告诉我。

coliru上看到它正在运行。

答案 4 :(得分:3)

不完全是你问的但是......只是为了好玩,使用std::is_base_of你可以创建(至少在C ++ 14中)一个与你的结构类似的constexpr函数。

以下是一个工作示例(仅限C ++ 14)

#include <tuple>
#include <iostream>
#include <type_traits>

template <typename ... Ts>
struct foo : std::tuple<Ts>...
 { };

template <typename ... Ts1, typename ... Ts2>
bool isSubsetOf (std::tuple<Ts1...> const &, std::tuple<Ts2...> const &)
 {
   bool ret { true };

   using un = int[];
   using d2 = foo<Ts2...>;

   (void)un { (ret &= std::is_base_of<std::tuple<Ts1>, d2>::value, 0)... };

   return ret;
 }


int main()
 {
    using t1 = std::tuple<int, double>;
    using t2 = std::tuple<double, int>;
    using t3 = std::tuple<int, double, char>;

    std::cout << isSubsetOf(t1{}, t1{}) << std::endl;  // print 1
    std::cout << isSubsetOf(t1{}, t2{}) << std::endl;  // print 1
    std::cout << isSubsetOf(t2{}, t1{}) << std::endl;  // print 1
    std::cout << isSubsetOf(t1{}, t3{}) << std::endl;  // print 1
    std::cout << isSubsetOf(t3{}, t1{}) << std::endl;  // print 0
 }

答案 5 :(得分:3)

constexpr bool any_of() { return false; }
template<class...Bools>
constexpr bool any_of( bool b, Bools... bools ) {
  return b || any_of(bools...);
}
constexpr bool all_of() { return true; }
template<class...Bools>
constexpr bool all_of( bool b, Bools...bools ) {
  return b && all_of(bools...);
}
template<class T0, class...Ts>
struct contains_t : std::integral_constant<bool,
  any_of( std::is_same<T0, Ts>::value... )
> {};

template<class Tuple0, class Tuple1>
struct tuple_subset_of;

template<class...T0s, class...T1s>
struct tuple_subset_of< std::tuple<T0s...>, std::tuple<T1s...> >:
  std::integral_constant<bool,
    all_of( contains_t<T0s, T1s...>::value... )
  >
{};

Live example

这旨在允许在C ++之后轻松改进17 - 用折叠表达式替换any_ofall_of递归实现。

答案 6 :(得分:2)

我想我会戴上帽子。这是一个像OP要求的C ++ 11解决方案,我意识到C ++ 17具有更好的功能。它是一种仅限类型的解决方案(没有明确的static const bool或类似内容,只有true_typefalse_type,它们都有自己的内部bool

缺点是此解决方案迫使我实施logical_orlogical_and,我们会以conjunctiondisjunction的形式获得for free in C++17 )。

奇迹般地,代码比Maartan Barnelis' solution稍短,但可读性较差

namespace detail
{
template<class T, class U>
struct logical_or : std::true_type{};

template<>
struct logical_or<std::false_type, std::false_type> : std::false_type{};

template<class...>
struct logical_and : std::false_type{};

template<>
struct logical_and<std::true_type, std::true_type> : std::true_type{};
}

template<class...>
struct contains : std::false_type{};

template<class T>
struct contains<T, T> : std::true_type{};

template<class Type, class Types2Head, class... Types2>
struct contains<Type, Types2Head, Types2...> : detail::logical_or<typename std::is_same<Type, Types2Head>::type, typename contains<Type, Types2...>::type>{};

template<class...>
struct is_subset_of : std::false_type{};

template<class Type1, class... Types2>
struct is_subset_of<std::tuple<Type1>, std::tuple<Types2...>> : contains<Type1, Types2...>{};

template<class Type1Head, class... Types1, class... Types2>
struct is_subset_of<std::tuple<Type1Head, Types1...>, std::tuple<Types2...>> : detail::logical_and<typename contains<Type1Head, Types2...>::type, typename is_subset_of<std::tuple<Types1...>, std::tuple<Types2...>>::type>{};

Demo

答案 7 :(得分:2)

一个(稍微)严肃的答案(比前一个):使用Skypjack显示的技巧(谢谢!),你可以避免ContainsTypeContainsTypes的递归。

以下是一个工作示例(不仅适用于std::tuple,而且适用于通用(也是不同的)类型容器。

#include <tuple>
#include <type_traits>

template <typename T, typename ... Ts>
struct cType
{
    static const bool value {
        ! std::is_same<std::integer_sequence<bool,
                          false, std::is_same<T, Ts>::value...>,
                       std::integer_sequence<bool,
                          std::is_same<T, Ts>::value..., false>>::value };
 };

template <typename, typename>
struct isSubsetOf : std::false_type
 { };

template <template <typename...> class C1, template <typename...> class C2,
          typename ... Ts1, typename ... Ts2>
struct isSubsetOf<C1<Ts1...>, C2<Ts2...>>
    : std::integral_constant<bool,
         std::is_same<std::integer_sequence<bool,
                         true, cType<Ts1, Ts2...>::value...>,
                      std::integer_sequence<bool,
                         cType<Ts1, Ts2...>::value..., true>
                   >::value>
 { };


int main()
{
    using t1 = std::tuple<int, double>;
    using t2 = std::tuple<double, int>;
    using t3 = std::tuple<int, double, char>;

    static_assert(isSubsetOf<t1, t1>::value, "err");
    static_assert(isSubsetOf<t1, t2>::value, "err");
    static_assert(isSubsetOf<t2, t1>::value, "err");
    static_assert(isSubsetOf<t2, t3>::value, "err");
    static_assert(!isSubsetOf<t3, t2>::value, "err");
}

此示例使用std::integer_sequence这是一个C ++ 14功能,但创建C ++ 11替换是很简单的,例如:

template <typename T, T ... ts>
struct integerSequence
{ };

答案 8 :(得分:2)

您之前提问的答案的

is_subset_of版本:

#include <tuple>
#include <type_traits>

template <class T>
struct tag { };

template <class... Ts>
struct is_subset_of_helper: tag<Ts>... { };

template <class, class, class = void>
struct is_subset_of: std::false_type { };

template <bool...>
struct bool_pack { };

template <bool... Bs>
using my_and = std::is_same<bool_pack<Bs..., true>, bool_pack<true, Bs...>>;

template <class... Ts1, class... Ts2>
struct is_subset_of<std::tuple<Ts1...>, std::tuple<Ts2...>, typename std::enable_if< my_and< std::is_base_of<tag<Ts1>, is_subset_of_helper<Ts2...>>::value...  >::value  >::type  >:
   std::true_type { };

int main() {    
    using t1 = std::tuple<int, double>;
    using t2 = std::tuple<double, int>;
    using t3 = std::tuple<int, double, char>;

    static_assert(is_subset_of<t1, t1>::value, "err");
    static_assert(is_subset_of<t1, t2>::value, "err");
    static_assert(is_subset_of<t2, t1>::value, "err");
    static_assert(is_subset_of<t2, t3>::value, "err");
    static_assert(!is_subset_of<t3, t2>::value, "err");
}

[live demo]