Struct Flattening
tama
repeatedly at gmail.com
Tue Apr 21 22:50:58 PDT 2009
On Wed, 22 Apr 2009 13:42:27 +0900, dsimcha <dsimcha at yahoo.com> 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 <repeatedly at gmail.com>
http://profile.livedoor.com/repeatedly/
メンバー募集中
http://tpf.techtalk.jp/
More information about the Digitalmars-d
mailing list