Thread overview
Converting a C MACRO in D
Jul 15, 2004
Pac
Jul 15, 2004
Vathix
Jul 16, 2004
Norbert Nemec
Jul 16, 2004
Matthew Wilson
Jul 16, 2004
Andy Friesen
July 15, 2004
Hi,


it is written on the D web site (http://www.digitalmars.com//d/htomodule.html) that macros like

#define MAX(a,b) ((a) < (b) ? (b) : (a))

should be remplaced by :

int MAX(int a, int b) { return (a < b) ? b : a); }

But what if we want real, float or double type for the arguments ? iS it possible to overload the function ?

Best regards

Pac
July 15, 2004
"Pac" <Pac_member@pathlink.com> wrote in message news:cd6hmp$2am7$1@digitaldaemon.com...
> Hi,
>
>
> it is written on the D web site
(http://www.digitalmars.com//d/htomodule.html)
> that macros like
>
> #define MAX(a,b) ((a) < (b) ? (b) : (a))
>
> should be remplaced by :
>
> int MAX(int a, int b) { return (a < b) ? b : a); }
>
> But what if we want real, float or double type for the arguments ? iS it possible to overload the function ?
>
> Best regards
>
> Pac

Yes, you can overload functions. But in this case, a template might be better:


template MAX(T)
{
   T MAX(T a, T b)  { return (a < b) ? b : a); }
}

int foo = MAX!(int)(1, 2);
float bar = MAX!(float)(1.0, 2.0);


July 16, 2004
Vathix wrote:

> "Pac" <Pac_member@pathlink.com> wrote in message news:cd6hmp$2am7$1@digitaldaemon.com...
>> Hi,
>>
>>
>> it is written on the D web site
> (http://www.digitalmars.com//d/htomodule.html)
>> that macros like
>>
>> #define MAX(a,b) ((a) < (b) ? (b) : (a))
>>
>> should be remplaced by :
>>
>> int MAX(int a, int b) { return (a < b) ? b : a); }
>>
>> But what if we want real, float or double type for the arguments ? iS it possible to overload the function ?
>>
>> Best regards
>>
>> Pac
> 
> Yes, you can overload functions. But in this case, a template might be better:
> 
> 
> template MAX(T)
> {
>    T MAX(T a, T b)  { return (a < b) ? b : a); }
> }
> 
> int foo = MAX!(int)(1, 2);
> float bar = MAX!(float)(1.0, 2.0);

... once again crying out for implicit instantiation of function templates. :-)
July 16, 2004
"Norbert Nemec" <Norbert.Nemec@gmx.de> wrote in message news:cd7toq$2spk$1@digitaldaemon.com...
> Vathix wrote:
>
> > "Pac" <Pac_member@pathlink.com> wrote in message news:cd6hmp$2am7$1@digitaldaemon.com...
> >> Hi,
> >>
> >>
> >> it is written on the D web site
> > (http://www.digitalmars.com//d/htomodule.html)
> >> that macros like
> >>
> >> #define MAX(a,b) ((a) < (b) ? (b) : (a))
> >>
> >> should be remplaced by :
> >>
> >> int MAX(int a, int b) { return (a < b) ? b : a); }
> >>
> >> But what if we want real, float or double type for the arguments ? iS it possible to overload the function ?
> >>
> >> Best regards
> >>
> >> Pac
> >
> > Yes, you can overload functions. But in this case, a template might be better:
> >
> >
> > template MAX(T)
> > {
> >    T MAX(T a, T b)  { return (a < b) ? b : a); }
> > }
> >
> > int foo = MAX!(int)(1, 2);
> > float bar = MAX!(float)(1.0, 2.0);
>
> ... once again crying out for implicit instantiation of function templates. :-)

Hey, you should see the mess I'm in with DTL without impl ex.! Nonetheless, the fog's are slowly clearing ... :)



July 16, 2004
Norbert Nemec wrote:
>>Yes, you can overload functions. But in this case, a template might be
>>better:
>>
>>
>>template MAX(T)
>>{
>>   T MAX(T a, T b)  { return (a < b) ? b : a); }
>>}
>>
>>int foo = MAX!(int)(1, 2);
>>float bar = MAX!(float)(1.0, 2.0);
> 
> 
> ... once again crying out for implicit instantiation of function
> templates. :-)

And how: <http://andy.tadan.us/d/tuple.d>

 -- andy