1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
/*
   IsMemberInSet is a template helper class that allows
   to determine at compile-time if a non-negative integers
   is included in a set of non-negative integers.

   The first parameter of the template is the integer
   to be tested for membership, all other parameters
   specify the actual set.

   Examples:

      * IsMemberInSet<2, 0, 1, 2, 3>::is_member is true
        as 2 is included in {0, 1, 2, 3}

      * IsMemberInSet<2>::is_member is false
        as 2 is not member of {}
*/

#ifndef SET_HPP
#define SET_HPP

template<unsigned int candidate, unsigned int ... members>
struct IsMemberInSet;

/* partial template specialization for the empty set which
   ends the recursion */
template<unsigned int candidate>
struct IsMemberInSet<candidate> {
   public:
      static constexpr bool is_member = false;
};

/* partial template specialization for the recursive case where
   we compare the candidate with the first member of the set and then
   recursively with the rest of the set */
template<unsigned int candidate, unsigned int first_member,
   unsigned int ... other_members>
struct IsMemberInSet<candidate, first_member, other_members...> {
   public:
      static constexpr bool is_member = (candidate == first_member) ||
              IsMembercandidatecandiother_membersembers...>::is_member;
};

#endif