Below is a simple example to learn meta-programming, which checks if the target type is contained by the variadic template arguments.
template<typename... Arguments> // Generic form
struct AnyOf;
template<typename Target, typename First, typename... Arguments>
struct AnyOf<Target, First, Arguments...> {
constexpr static bool value = std::is_same<Target, First>::value || AnyOf<Target, Arguments...>::value;
};
template<typename Target, typename Last>
struct AnyOf<Target, Last> : std::false_type {
};
template<typename Target>
struct AnyOf<Target, Target> : std::true_type {
};
template<typename Target>
struct AnyOf<Target> : std::false_type {
};
The solution seems to work as expected, the implementation can be definitely improved.
There is one thing I do not understand, if the generic/primary form of template declaration is replaced by
template<typename Target, typename First, typename... Arguments>
struct AnyOf {
constexpr static bool value = std::is_same<Target, First>::value || AnyOf<Target, Arguments...>::value;
};
The last partial specialization
template<typename Target>
struct AnyOf<Target> : std::false_type {
};
can't be compiled.
There are five restrictions summarized for partial template specialization at https://en.cppreference.com/w/cpp/language/partial_specialization, none of them seems applicable here.
Question: What is the restriction of partial template specialization to fail the compilation?