/*
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 |