मैं Adam's code में कुछ सुधार किए कि बंद टपल के पहले एन तर्क पट्टी होगा, साथ ही केवल पिछले एन प्रकार के साथ एक नया टपल बनाने ... यहाँ पूरा कोड (नोट है: किसी को भी मेरा उत्तर +1 करने के लिए, यह भी निर्णय लेता है तो कृपया +1 कि जब एडम जवाब क्या इस कोड पर आधारित है, और मैं किसी भी क्रेडिट उनके योगदान से दूर ले करने के लिए) इच्छा नहीं है:
//create a struct that allows us to create a new tupe-type with the first
//N types truncated from the front
template<size_t N, typename Tuple_Type>
struct tuple_trunc {};
template<size_t N, typename Head, typename... Tail>
struct tuple_trunc<N, std::tuple<Head, Tail...>>
{
typedef typename tuple_trunc<N-1, std::tuple<Tail...>>::type type;
};
template<typename Head, typename... Tail>
struct tuple_trunc<0, std::tuple<Head, Tail...>>
{
typedef std::tuple<Head, Tail...> type;
};
/*-------Begin Adam's Code-----------
Note the code has been slightly modified ... I didn't see the need for the extra
variadic templates in the "assign" structure. Hopefully this doesn't break something
I didn't forsee
*/
template<size_t N, size_t I>
struct assign
{
template<class ResultTuple, class SrcTuple>
static void x(ResultTuple& t, const SrcTuple& tup)
{
std::get<I - N>(t) = std::get<I>(tup);
assign<N, I - 1>::x(t, tup); //this offsets the assignment index by N
}
};
template<size_t N>
struct assign<N, 1>
{
template<class ResultTuple, class SrcTuple>
static void x(ResultTuple& t, const SrcTuple& tup)
{
std::get<0>(t) = std::get<1>(tup);
}
};
template<size_t TruncSize, class Tup> struct th2;
//modifications to this class change "type" to the new truncated tuple type
//as well as modifying the template arguments to assign
template<size_t TruncSize, class Head, class... Tail>
struct th2<TruncSize, std::tuple<Head, Tail...>>
{
typedef typename tuple_trunc<TruncSize, std::tuple<Head, Tail...>>::type type;
static type tail(const std::tuple<Head, Tail...>& tup)
{
type t;
assign<TruncSize, std::tuple_size<type>::value>::x(t, tup);
return t;
}
};
template<size_t TruncSize, class Tup>
typename th2<TruncSize, Tup>::type tail(const Tup& tup)
{
return th2<TruncSize, Tup>::tail(tup);
}
//a small example
int main()
{
std::tuple<double, double, int, double> test(1, 2, 3, 4);
tuple_trunc<2, std::tuple<double, double, int, double>>::type c = tail<2>(test);
return 0;
}
आप सच variadic टेम्पलेट समर्थन है ('टेम्पलेट <वर्ग ... args>')? साथ ही, क्या आप मानों की प्रतिलिपि बनाना चाहते हैं या आप उनमें से * दृश्य * चाहते हैं, जैसा मूल में संदर्भ हैं? – Xeo
@Xeo: मुझे संदर्भ बेहतर चाहिए, लेकिन यह वास्तव में कोई फर्क नहीं पड़ता। – RiaD
@Xeo: अब मैं विविध टेम्पलेट्स का उपयोग नहीं करता हूं। लेकिन मैं g ++ 4.6 पर C++ 0x का उपयोग करता हूं और मुझे लगता है कि वे समर्थित हैं। – RiaD