import std.stdio; import std.string; template Tuple( T... ) { align(1) struct Tuple { static if( T.length > 0 ) { T[0] first; } static if( T.length > 1 ) { T[1] second; } static if( T.length > 2 ) { T[2] third; } static if( T.length > 3 ) { T[3] fourth; } static if( T.length > 4 ) { T[4] fifth; } static if( T.length > 5 ) { T[5] sixth; } static if( T.length > 6 ) { T[6] seventh; } static if( T.length > 7 ) { T[7] eighth; } static if( T.length > 8 ) { T[8] ninth; } // ... etc if more than 9 elements are needed } } byte[] pack(T...)( T tuple ) { int size = 0; foreach( item; tuple ) size += item.sizeof; byte[] result = new byte[size]; int position = 0; foreach( item; tuple ) { result[position..position+item.sizeof] = (cast(byte*)&item)[0..item.sizeof]; position += item.sizeof; } return result; } Tuple!(T) unpack(T...)( byte[] data ) { Tuple!(T) result; assert( data.length == result.sizeof ); result = *(cast(Tuple!(T)*)data.ptr); return result; } void main() { byte[] packed = pack!(char,short,float)( 'a', 1, 2.3 ); auto unpacked = unpack!(char,short,float)( packed ); foreach( i; unpacked.tupleof ) writefln( i ); }