Variadic Template(七)
使用组合的方式实现tuple的功能
#include<iostream> using namespace std; template<typename... Values> class tup; template<> class tup<>{}; // to realize function of tuple by using compistion template<typename Head, typename... Tail> class tup<Head, Tail...> { typedef tup<Tail...> composited; protected: composited m_tail; Head m_head; public: tup(){}; tup(Head v, Tail... vtail) :m_tail(vtail...), m_head(v){} Head head(){ return m_head; } composited& tail(){ return m_tail; } }; int main() { tup<int, float, string>it1(41,6.3,"nico"); cout << "sizeof(it1) = " << sizeof(it1) << endl; cout << "it1.head() = " << it1.head() << endl; cout << "it1.tail().head() = " << it1.tail().head() << endl; cout << "it1.tail().tail().head() = " << it1.tail().tail().head() << endl; return 0; }