Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 29, 2016 Joining AliasSeq/TypeTuple s | ||||
---|---|---|---|---|
| ||||
Hi, i want to join two or more tupples in to one, with mixing the indexes like roundRobin but in compile time. unittest{ import std.meta; alias first=AliasSeq!(int, string,bool); alias second=AliasSeq!("abc","def","ghi"); alias third=... static assert(third==AliasSeq!(int, "abc", string, "def", bool, "ghi")); } How to obtain this kind of functionality ? Is there maybe some builin template for this in phobos ? I couldn't find this. Thank you Voitech. |
March 29, 2016 Re: Joining AliasSeq/TypeTuple s | ||||
---|---|---|---|---|
| ||||
Posted in reply to Voitech | On Tuesday, 29 March 2016 at 09:33:40 UTC, Voitech wrote:
> Hi, i want to join two or more tupples in to one, with mixing the indexes like roundRobin but in compile time.
>
> unittest{
> import std.meta;
> alias first=AliasSeq!(int, string,bool);
> alias second=AliasSeq!("abc","def","ghi");
> alias third=...
>
> static assert(third==AliasSeq!(int, "abc", string, "def", bool, "ghi"));
> }
>
>
> How to obtain this kind of functionality ? Is there maybe some builin template for this in phobos ? I couldn't find this.
>
> Thank you
> Voitech.
import std.meta : AliasSeq;
template RR(A...)
if (!(A.length & 1))
{
static if (A.length == 0)
alias RR = AliasSeq!();
else {
alias Left = A[0 .. $ / 2];
alias Right = A[$ / 2 .. $];
alias RR = AliasSeq!(Left[0], Right[0], RR!(Left[1 .. $], Right[1 .. $]));
}
}
struct S(A...) {} // needed to reliably compare AliasSeq's for equality
unittest {
alias first = AliasSeq!(int, string, bool);
alias second = AliasSeq!("abc", "def", "ghi");
alias third = RR!(first, second);
static assert(is(S!third == S!(int, "abc", string, "def", bool, "ghi")));
}
|
March 29, 2016 Re: Joining AliasSeq/TypeTuple s | ||||
---|---|---|---|---|
| ||||
Posted in reply to Voitech | On 03/29/2016 02:33 AM, Voitech wrote:
> Hi, i want to join two or more tupples in to one, with mixing the
> indexes like roundRobin but in compile time.
>
> unittest{
> import std.meta;
> alias first=AliasSeq!(int, string,bool);
> alias second=AliasSeq!("abc","def","ghi");
> alias third=...
>
> static assert(third==AliasSeq!(int, "abc", string, "def", bool, "ghi"));
> }
Just a reminder that if you needed to concatenate, then it would be trivial due to automatic expansion of AliasSeq elements:
alias third = AliasSeq!(first, second);
Ali
|
March 30, 2016 Re: Joining AliasSeq/TypeTuple s | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adrian Matoga | On Tuesday, 29 March 2016 at 10:06:43 UTC, Adrian Matoga wrote:
> (...)
Another version that doesn't misbehave if first and second are of different lengths:
import std.meta : AliasSeq;
template RR(A...) {
template With(B...) {
static if (A.length == 0 || B.length == 0)
alias With = AliasSeq!(A, B); // or static assert(0) if you require equal lengths
else
alias With = AliasSeq!(A[0], B[0], RR!(A[1 .. $]).With!(B[1 .. $]));
}
}
struct S(A...) {} // needed to reliably compare AliasSeq's for equality
unittest {
alias first = AliasSeq!(int, string, bool);
alias second = AliasSeq!("abc", "def", "ghi");
alias third = RR!first.With!second;
static assert(is(S!third == S!(int, "abc", string, "def", bool, "ghi")));
alias fourth = RR!(first[0 .. 2]).With!second;
static assert(is(S!fourth == S!(int, "abc", string, "def", "ghi")));
}
|
Copyright © 1999-2021 by the D Language Foundation