C++ Macro to D mixin template, delegates Please help

Henning Hasemann hhasemann at web.de
Fri Sep 21 03:35:24 PDT 2007


Thanks for all the compliments :)


> Just one question : It seems that you use template overloading, or do
> I miss something. In other words : Can I use template overloading
> like function overloading ?

Somehow you can do stuff like this for example:

template Foo(int n, T) {
  // some stuff
}

template Foo(T) {
  // something else
}

You can even define templates differently for different types, but
thats called "specialisation" not overloading.

But I dont use such a thing here. What I do is defining a few templated
structs and the structs (not instances of them, the actual type!) are
given to MessageMap which calls the static functions.

So technically, OnClose!(foo) and OnRange!(1, 3, bar) are *types* that
contain aliases (think of compile time references) to the methods you
want to call.

The "mixin MessageMap!(...)" simply adds a new method to your class.
Note that because it is run over types, the "foreach(mapping; Mappings)
{ .. }" is done at compile time, ie its body is repeated for every type
(I called them Mapping's here), so all runtime overhead you have are
the ifs that evaluate to false.

If you have further questions, dont hesitate to ask :)

Henning

-- 
GPG Public Key:
http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851
Fingerprint: 344F 4072 F038 BB9E B35D  E6AB DDD6 D36D 4191 1851


More information about the Digitalmars-d-learn mailing list