I needed to do something similar for listings. I have a variable and want to test it against ranges of values.
Here I used the variational function of the template. Note the specialization for the const char* , so is_in( my_str, "a", "b", "c") has the expected result when my_str stores "a" .
#include <cstring> template<typename T> constexpr bool is_in(T t, T v) { return t == v; } template<> constexpr bool is_in(const char* t, const char* v) { return std::strcmp(t,v); } template<typename T, typename... Args> constexpr bool is_in(T t, T v, Args... args) { return t==v || is_in(t,args...); }
Usage example:
enum class day { mon, tues, wed, thur, fri, sat, sun }; bool is_weekend(day d) { return is_in(d, day::sat, day::sun); }
Darren smith
source share