这是一个解决方案:而不是截断N
从后面看,我只是截断sizeof...(Args) - N
从前面:
#include <tuple>
/* Concatenator helper */
template <typename T, typename Tuple> struct cat;
template <typename T, typename ...Args>
struct cat<T, std::tuple<Args...>>
{
typedef typename std::tuple<T, Args...> value;
};
/* Head-of-tuple */
template <unsigned int, typename...> struct tuple_head;
// Base case. Need to specialize twice, once for one and once for variadic types
template <typename ...Args>
struct tuple_head<0, Args...>
{
typedef std::tuple<> value;
};
template <typename T>
struct tuple_head<0, T>
{
typedef std::tuple<> value;
};
// Recursion step
template <unsigned int N, typename T, typename ...Args>
struct tuple_head<N, T, Args...>
{
typedef typename cat<T, typename tuple_head<N - 1, Args...>::value>::value value;
};
/* User interface */
template <unsigned int N, typename ...Args>
struct PartialTuple
{
typedef typename tuple_head<sizeof...(Args) - N, Args...>::value type;
};
/* Usage */
#include <string>
int main()
{
// I want this to not work...
//PartialTuple<1, std::string, std::string, int, int>::type A{"test", 5, 1};
// I want this to work...
PartialTuple<1, std::string, std::string, int, int>::type B("test", "test", 5);
PartialTuple<0, std::string, std::string, int, int>::type C("test", "test", 5, 6);
}