------- Comment #11 from jack dot q dot word at gmail dot com 2009-08-13 05:14 ------- (From update of attachment 18348) template< class cT, class... vT > struct VarTypeNav { typedef cT T; typedef VarTypeNav< vT..., void > Next; struct scT { cT v; }; static const size_t Size = sizeof(scT) + Next::Size; static const size_t Count = 1 + Next::Count; };
template< class... vT > struct VarTypeNav< void, vT... > { typedef void T; typedef void Next; static const size_t Size = 0; static const size_t Count = 0; }; template< const size_t nIndex, class cT, class... vT > struct VarTypeSelect { typedef VarTypeSelect< nIndex-1, vT... >::T T; struct scT { T v; }; static const size_t Offset = sizeof(scT) + VarTypeSelect< nIndex-1, vT... >::Offset; }; template< class cT, class... vT > struct VarTypeSelect< 0, cT, vT... > { typedef cT T; static const size_t Offset = 0; }; template< class cT, class... vT > struct Tuple { char Data[VarTypeNav<cT,vT...>::Size]; template< const size_t index > VarTypeSelect< index, vT... >::T & operator [] ( const size_t index ) { static_assert(!(index > VarTypeNav< vT... >::Count ), "Index out of bounds"); char * pThisChars = (char *) this; return *( (VarTypeSelect< index, vT... >::T *) pThisChars[ VarTypeSelect< index, vT... >::Offset ])); } template< const size_t index > const VarTypeSelect< index, vT... >::T & operator [] ( const size_t index ) const { static_assert(!(index > VarTypeNav< vT... >::Count ), "Index out of bounds"); const char * pThisChars = (const char *) this; return *( (const VarTypeSelect< index, vT... >::T *) pThisChars[ VarTypeSelect< index, vT... >::Offset ])); } }; int main() { Tuple< size_t, char * > x; /* = { 32, new char[32] }; *//* ? */ x[0] = 32; x[1] = new char[32]; x[1][31] = 0; return (int) &x[1][0] != 0; } -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35722