April 11, 2015 Re: Make a type tuple from an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On 04/10/15 17:36, John Colvin via Digitalmars-d-learn wrote: > On Friday, 10 April 2015 at 15:13:54 UTC, Marc Schütz wrote: >> Is there a way to turn an array (known at compile time) into a TypeTuple? > For input ranges in general: > > import std.range : isInputRange; > > template TypeTupleOf(TL...) > if (TL.length == 1 && isInputRange!(typeof(TL[0]))) > { > import std.typetuple : TT = TypeTuple; > enum r = TL[0]; > static if (r.empty) > alias TypeTupleOf = TT!(); > else > { > enum f = r.front; > alias TypeTupleOf = TT!( > f, > TypeTupleOf!( > { auto tmp = r; tmp.popFront(); return tmp; }() > ) > ); > } > } Neat, but very unreadable... import std.array, std.range : isInputRange, dropOne; template TypeTupleOf(alias R) if (isInputRange!(typeof(R))) { import std.typetuple : TT = TypeTuple; static if (R.empty) alias TypeTupleOf = TT!(); else alias TypeTupleOf = TT!(R.front(), TypeTupleOf!(R.dropOne())); } artur |
Copyright © 1999-2021 by the D Language Foundation