I think the template definitions are wrong, in both cases you invoke exact recursion. I would expect the compiler to die with some stackoverflow inside the compiler, but another error occurs ...
The implementation of the are_same variational template can be:
template <class... Args> // base (optional to declare the template) struct are_same; template <class A, class B, class... Args> // recursion struct are_same<A,B,Args...> { static const bool value = is_same<A,B>::value && are_same<A,Args...>::value; }; template <class A, class B> // stop condition struct are_same<A,B> { static const bool value = is_same<A,B>::value; };
Note that in the recursion step recursion one argument is removed from the argument list, so a new problem to solve is a smaller version of the original. This type of metaprogramming of templates is quite related to recursion, and the same rules apply to be able to use the recursion necessary so that each recursive step brings you closer to the solution. In this particular case, given a list of N possible identical types, each step reduces the problem to determine if the N-1 types are the same.
You can use alternatively, as a stop condition (replacing the former), a degenerate version of the are_same problem:
template <class A> struct are_same<A> { static const bool value = true; };
Which is degenerate in the sense that there really is no point in asking if one type is * is_same *, but it might be appropriate for different metaprogramming tasks.
Another potentially more efficient algorithm (I'm not sure that the compiler will avoid creating a template in the previous recursion step), which is not dependent on is_same , might be:
template <class... Args> struct are_same; template <class A, class... Args> struct are_same<A,A,Args...> {
In this case, the compiler will prefer recursion for cut steps whenever the two types are the same, so we do not need to check is_same internally. At the same time, if the compiler goes to the cut step, we do not need to process the rest of the type list, as we already know the answer.