The following almost work (and for the last N arguments instead of the first, but hey). Maybe someone can help with a compilation error in the comments below:
#include <iostream> void foo (int a, int b) { std :: cout << "3 args: " << a << " " << b << "\n"; } void foo (int a, int b, int c) { std :: cout << "3 args: " << a << " " << b << " " << c << "\n"; } template <int n, typename... Args> struct CallFooWithout; template <typename... Args> struct CallFooWithout <0, Args...> { static void call (Args... args) { foo (args...); } }; template <int N, typename T, typename... Args> struct CallFooWithout <N, T, Args...> { static void call (T, Args... args) { CallFooWithout <N-1, Args...> :: call (args...); // ambiguous class template instantiation for 'struct CallFooWithout<0, int, int, int>' // candidates are: struct CallFooWithout<0, Args ...> // struct CallFooWithout<N, T, Args ...> } }; template <int n, typename... Args> void call_foo_with_last (Args... args) { CallFooWithout <sizeof...(Args)-n, Args...> :: call (args...); } int main () { call_foo_with_last <2> (101, 102, 103, 104, 105); call_foo_with_last <3> (101, 102, 103, 104, 105); }
I donβt understand why this is ambiguous, because 0 is more specialized than N, so it should satisfy partial ordering?!?!?
On the contrary, the following is wonderful.
template <int N, typename... T> struct Factorial { enum { value = N * Factorial<N - 1,T...>::value }; }; template <typename... T> struct Factorial<0, T...> { enum { value = 1 }; }; void foo() { int x = Factorial<4,int>::value; }
What's the difference?