A library similar to boost::bind

Tom S h3r3tic at remove.mat.uni.torun.pl
Fri Jul 7 09:02:12 PDT 2006


Lutger wrote:
> I'm a little late, but thanks!!! I was hoping somebody would do this. It works
> like a charm. 
> 
> I tried studying the source, and while it's a LOT better to follow than boost, I
> discovered I don't understand metaprogramming at all (yet), unfortunatly.
> Can I ask you, how can I use the Tuple type and what are it's features?
> This much I figured out:
> 
> Tuple!(int,int) someTuple;
> someTuple.val!(1) = 5;
> assert(someTuple.val!(1) == 5);

Thanks for testing and kind words :)
I've uploaded an updated version at the same address:
http://158.75.59.9/~h3/code/bind.rar


As for the Tuple's features, it's really pretty basic, but at the same 
time quite powerful and allowing for the creation of many interesting 
templates. I'll use an example to demonstrate its features:


Tuple!(int, float) a;
a.val!(0) = 5;
a.val!(1) = 2.3f;

// simple printing
writefln(a.toString);


// various insertion ops
auto b = a.prepend!(char[])("foo");
writefln(b.toString);

auto c = b.append!(double)(1.234567);
writefln(c.toString);

auto d = c.insertAfter!(1, int)(1234);
writefln(d.toString);


// .length property like in arrays
writefln("d.length: ", d.length);


// behaves like a struct
auto e = d.insertBefore!(1, char[])("another string...");
auto f = e;
f.val!(1) = "modified !";

writefln(e.toString);
writefln(f.toString);


// direct access to fields. the '.val' is an templated alias, not an 
accessor function
f.val!(3) += 2;
writefln(typeid(typeof(f.val!(3))));
writefln(f.toString);


struct Test {
	int x;
	float y;
	double z;
	char w;
}

// no extra costs
static assert(Test.sizeof == (Tuple!(int, float, double, char)).sizeof);

// easy creation. // "".dup is used because we don't like static arrays.
writefln(makeTuple(1, 2.2, 5.f, "foo".dup, 2+7i, "bar".dup).toString);

// nest 'em as you like
writefln(makeTuple(makeTuple(1, 2), makeTuple(3, 4)).toString);



-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/



More information about the Digitalmars-d-announce mailing list