Comparing Two Type Tuples

Justin Spahr-Summers Justin.SpahrSummers at gmail.com
Sun Apr 4 14:23:06 PDT 2010


On Sun, 4 Apr 2010 21:05:49 +0000 (UTC), Daniel Ribeiro Maciel 
<danielmaciel at gmail.com> wrote:
> 
> Heya ppl!
> 
> I was wondering how could I write a function that takes two Type
> Tuples as arguments and returns true if they are match.
> 
> Could anyone help me with this?
> 
> Thanks!

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 .. $]));
}

void main() {
    alias TypeTuple!(int, double, char[]) tupleA;
    alias TypeTuple!(int, double, char[]) tupleB;
    alias TypeTuple!(int, double, char*) tupleC;
    
    compare!(tupleA.length, tupleA, tupleB);
    compare!(tupleA.length, tupleA, tupleC);
}

will output "true" then "false."


More information about the Digitalmars-d-learn mailing list