Comparing Two Type Tuples

Philippe Sigaud philippe.sigaud at gmail.com
Mon Apr 5 03:52:50 PDT 2010


On Sun, Apr 4, 2010 at 23:23, Justin Spahr-Summers <
Justin.SpahrSummers at gmail.com> wrote:

> You can really only pass a single type tuple to a template, but you can
> work around it by passing a length and then comparing two parts of a
> combined tuple. For instance:
>
> import std.stdio;
> import std.typetuple;
>
> void compare(uint LEN, TL ...) () {
>    writefln("%s", is(TL[0 .. LEN] == TL[LEN .. $]));
> }
>
>
Since to be equal, they must have the same length, you can even get rid of
the 'len' part and cut in the middle:

bool compare(TT...)() {
    static if (TT.length % 2 == 0 && is(TT[0..$/2] == TT[$/2..$]))
        return true;
    else
        return false;
}

That's useful to compare two functions:

template sameArgs(alias fun1, alias fun2) {
    static if (compare!(ParameterTypeTuple!fun1, ParameterTypeTuple!fun2))
        enum bool sameArgs = true;
    else
        enum bool sameArgs = false;
}

(I think we could use std.traits.Select to manage this kind of static ifs)

  Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100405/f0a36794/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list