Passing Elements of A Static Array as Function Parameters

Per Nordlöw via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 14 02:09:26 PDT 2015


On Monday, 14 September 2015 at 07:05:23 UTC, Meta wrote:
> You could turn it into a Tuple and use the `expand` method to 
> get a TypeTuple (AliasSeq).
>
> import std.typecons;
> import std.typetuple;
> import std.stdio;
>
> template genTypeList(T, size_t n)
> {
> 	static if (n <= 1)
> 	{
> 		alias genTypeList = T;
> 	}
> 	else
> 	{
> 		alias genTypeList = TypeTuple!(T, genTypeList!(T, n - 1));
> 	}
> }
>
> auto asTuple(T, size_t n)(ref T[n] arr)
> {
> 	return Tuple!(genTypeList!(T, n))(arr);
> }
>
> void test(T...)(T xs)
> {
> 	writeln("Length: ", T.length, ", Elements: ", xs);
> }
>
> void main()
> {
> 	int[5] a = [0, 1, 2, 3, 4];
> 	test(a);                    //Length: 1, Elements: [0, 1, 2, 
> 3, 4]
> 	test(a.asTuple.expand);     //Length: 5, Elements: 01234
> }

Is there a reason why such a common thing isn't already in 
Phobos? If not what about adding it to std.typecons : asTuple


More information about the Digitalmars-d-learn mailing list