Question Re Templates & Tuples

Daniel Giddings dgiddings at bigworldtech.com
Mon Jan 15 19:25:48 PST 2007


First there seems to be an issue with copying tuples made from structs 
with .tupleof (sample code in test.d).

 > dmd -run test.d
12.3
0nan

I'm guessing that this is for similar reasons to tuples not being able 
to be returned from functions yet?

Which brings me to my question re templates... in pack.d I have 
templated a Tuple struct which can have varying numbers of properties. 
Is there a way of doing this where you don't have to write out the 
maximum number of elements manually?

The names aren't important as you can iterate over them with .tupleof.

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
	}
}

Running pack.d produces:

 > dmd -run pack.d
a
1
2.3

I'm only interested in packing PoD (plain old data types) at this stage.

Cheers,

:-) Dan


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.d
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070116/9f65ddac/attachment.ksh 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pack.d
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070116/9f65ddac/attachment.txt 


More information about the Digitalmars-d-learn mailing list