On Wed, 22 Apr 2009 13:42:27 +0900, dsimcha <[email protected]> wrote:

It's basically a tuple with some special properties.  Let's say I have a
template struct:

struct Joint(T...) {
   T ranges;
}

It's built with:

SomeType joint(T...)(T args) { // do stuff. }

When one of the arguments is a Joint, I want it to flatten.  For example,

Joint!(uint[], uint[]) r1;
uint[] r2;
auto result = joint(r1, r2);
// result is a Joint!(uint[], uint[], uint[]), not a
// Joint!(Joint!(uint[], uint[]), uint[]).

Is there an easy metaprogramming trick that I've overlooked to make stuff
flatten like this?

Hi,

---
import std.stdio, std.typetuple, std.traits;

struct Joint(T...)
{
    T ranges;
}

template flatten(T...)
{
    static if (T.length == 0)
        alias T flatten;
    else
        alias TypeTuple!(FieldTypeTuple!(T[0]), flatten!(T[1..$])) flatten;
}

Joint!(flatten!(T)) joint(T...)(T args)
{
    typeof(return) result;
    return result;
}

void main()
{

    Joint!(uint[], uint[]) r1;
    uint[] r2;
    auto result = joint(r1, r2);
    writeln(result);
}
---

Tested dmd 2.029 on Windows XP.

--
tama <[email protected]>
http://profile.livedoor.com/repeatedly/
メンバー募集中
http://tpf.techtalk.jp/

Reply via email to