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

BLS nanali at nospam-wanadoo.fr
Thu Sep 20 09:30:58 PDT 2007


Regan Heath schrieb:
> 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.
> 
// Means
template ON_WM_CLOSE(D)
{
     alias D delegate() dg;
     if (uID == WM_CLOSE)
     {
         lResult = dg();
         return lResult;
     }
}
// is legal ?


>> 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 am not willing to wait another year. My IDE proj. is allready a never 
ending story, and now I have to create a GUI for 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

Well I think I'll ask the Tango folks whether (and how) I can use the 
Signal/Slot stuff INSTEAD.
Bjoern


More information about the Digitalmars-d-learn mailing list