Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
July 15, 2004 Converting a C MACRO in D | ||||
---|---|---|---|---|
| ||||
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 Re: Converting a C MACRO in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pac | "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 Re: Converting a C MACRO in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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 Re: Converting a C MACRO in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | "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 Re: Converting a C MACRO in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Norbert Nemec | 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 |
Copyright © 1999-2021 by the D Language Foundation