www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Converting a C MACRO in D

reply Pac <Pac_member pathlink.com> writes:
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
Jul 15 2004
parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"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);
Jul 15 2004
parent reply Norbert Nemec <Norbert.Nemec gmx.de> writes:
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. :-)
Jul 15 2004
next sibling parent "Matthew Wilson" <admin.hat stlsoft.dot.org> writes:
"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 ... :)
Jul 16 2004
prev sibling parent Andy Friesen <andy ikagames.com> writes:
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
Jul 16 2004