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

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

    If you can use C++17 features, I highly recommend using Piotr Skotnicki's solution!

    I had to implement this functionality a while ago. I am just going to copy-paste the code I came up with at that point.

    I am not claiming that this is the best or most elegant way to implement this kind of check! I did not bother thinking about the edge cases too much; you may need to adapt the code to fit your requirements.

    To clarify: ContainsTypes checks if Rhs is a subset of Lhs.


      template 
      struct ContainsType;
    
      template 
      struct ContainsType, U>
      {
          static const bool VALUE = ContainsType, U>::VALUE;
      };
    
      template 
      struct ContainsType, T>
      {
          static const bool VALUE = true;
      };
    
      template 
      struct ContainsType, T>
      {
          static const bool VALUE = false;
      };
    
      // -----
    
      template 
      struct ContainsTypes;
    
      template 
      struct ContainsTypes>
      {
          static const bool VALUE = ContainsType::VALUE && ContainsTypes>::VALUE;
      };
    
      template 
      struct ContainsTypes>
      {
          static const bool VALUE = true;
      };
    

提交回复
热议问题