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

前端 未结 9 846
甜味超标
甜味超标 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:42

    Here's a C++17 answer that I believe is quite simpler than Piotr's answer:

    template 
    struct contains : std::disjunction...>{};
    
    template 
    struct is_subset_of : std::false_type{};
    
    template 
    struct is_subset_of, std::tuple> : std::conjunction...> {};
    

    Demo

    disjunction and conjunction are new type traits introduced in C++17. We can take advantage of these to check if at least one type in the second tuple matches "the next type" in the first tuple, which we use parameter pack expansion extensively for.

提交回复
热议问题