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

Regan Heath regan at netmail.co.nz
Thu Sep 20 08:58:34 PDT 2007


BLS wrote:
> Regan Heath schrieb:
>> BLS wrote:
>>> template ON_WM_CLOSE(D) (D delegate() dg)
>>> {
>>>     if (uID == WM_CLOSE)
>>>     {
>>>         lResult = dg();
>>>         return lResult;
>>>     }
>>> }
>>
>> This seems to be mixing template syntax (using "template") with 
>> template function syntax (using "(D)(D delegate() dg)".  I don't think 
>> you want a template function so, rather something like:
>>
>> template ON_WM_CLOSE(alias D)
>> {
>>     if (uID == WM_CLOSE)
>>     {
>>         lResult = D();
>>         return lResult;
>>     }
>> }
>>
>> (see the duffs_device example here 
>> http://www.digitalmars.com/d/template-mixin.html)
>>
>> However this wont work because a template body can only contain 
>> 'declarations' which means;
>>
>> Declaration:
>>         typedef Decl
>>         alias Decl
>>         Decl
>>
>> Decl:
>>         StorageClasses Decl
>>         BasicType Declarators ;
>>         BasicType Declarator FunctionBody
>>     AutoDeclaration
>>
>> So, you can put typedefs, aliases, variables and functions in a 
>> template, but, what you can't do is put an "if" statement in there.  
>> So, templates and mixins in D just can't achieve the same effect as 
>> the C/C++ define macro can.
>>
>> The planned/propsed D macro feature however, that should do what you 
>> want I reckon.
>>
>> Regan
> 
> NOOOOOOOOOOoooooooo, Shi* !
> So I guess
> template TFoo(D)
> {
>    alias D delegate() dg
> }
> 
> is also not allowed...

This one is allowed, 'alias' is a declaration.

> Hell, there should be a way to translate such a simple C++ macro without 
> without having so much trouble!!!

The proposed macro feature should be able to do it.

> I can not imagine that it is impossible. Probabely there exist a
> workaround. have to look at the Tango Signal/Slot implementation.. Imean
> they 've managed it.

Good idea.  I wonder how they're doing it.  It's entirely possible that 
I'm getting something wrong here, I'm no template master.

Regan


More information about the Digitalmars-d-learn mailing list