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

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

    Not exactly what you asked but... just for fun, using std::is_base_of you can create (in C++14, at least) a constexpr function that works like your struct.

    The following is a working example (only C++14)

    #include 
    #include 
    #include 
    
    template 
    struct foo : std::tuple...
     { };
    
    template 
    bool isSubsetOf (std::tuple const &, std::tuple const &)
     {
       bool ret { true };
    
       using un = int[];
       using d2 = foo;
    
       (void)un { (ret &= std::is_base_of, d2>::value, 0)... };
    
       return ret;
     }
    
    
    int main()
     {
        using t1 = std::tuple;
        using t2 = std::tuple;
        using t3 = std::tuple;
    
        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
     }
    

提交回复
热议问题