Variadic Template(六)
利用递归继承的方式实现tuple
#include<iostream> using namespace std; template<typename... Values> class tuples; template<> class tuples<>{}; template<typename Head, typename... Tail> class tuples<Head, Tail...> : private tuples<Tail...> { typedef tuples<Tail...> inherited; protected: Head m_head; public: tuples(); tuples(Head v, Tail... vtail) : m_head(v), inherited(vtail...){} auto head()->decltype(m_head) { return m_head; } inherited& tail(){ return *this; } }; int main() { tuples<int,float,string>t(41,6.3,"nico"); cout << "sizeof(t) : " << sizeof(t) << endl; cout << t.head() << endl; cout << t.tail().head() << endl; cout << t.tail().tail().head() << endl; return 0; }
问题1:sizeof(t)为什么是40呢?视频里说是12