Make a type tuple from an array
Artur Skawina via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Apr 11 06:31:31 PDT 2015
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
More information about the Digitalmars-d-learn
mailing list