Check if one set of types is a subset of the other

前端 未结 9 855
甜味超标
甜味超标 2021-01-01 17:54

How can one check if one parameter pack (interpreted as a set) is a subset of another one?

So far I only have the frame (using std::tuple), but no functionality.

9条回答
  •  没有蜡笔的小新
    2021-01-01 18:24

    I suppose I'll throw my hat in the ring. This is a C++11 solution like the OP asked for, I realize that C++17 has much nicer features. It's a type-only solution (no explicit static const bool or the like, only true_type and false_type, which have their own internal bool)

    The downside is that this solution forced me to implement logical_or and logical_and, which we'd get for free in C++17 in the form of conjunction and disjunction).

    Miraculously the code is a tad shorter than Maartan Barnelis' solution, though arguably less readable

    namespace detail
    {
    template
    struct logical_or : std::true_type{};
    
    template<>
    struct logical_or : std::false_type{};
    
    template
    struct logical_and : std::false_type{};
    
    template<>
    struct logical_and : std::true_type{};
    }
    
    template
    struct contains : std::false_type{};
    
    template
    struct contains : std::true_type{};
    
    template
    struct contains : detail::logical_or::type, typename contains::type>{};
    
    template
    struct is_subset_of : std::false_type{};
    
    template
    struct is_subset_of, std::tuple> : contains{};
    
    template
    struct is_subset_of, std::tuple> : detail::logical_and::type, typename is_subset_of, std::tuple>::type>{};
    

    Demo

提交回复
热议问题