digitalmars.D - Converting a C MACRO in D
- Pac <Pac_member pathlink.com> Jul 15 2004
- "Vathix" <vathixSpamFix dprogramming.com> Jul 15 2004
- Norbert Nemec <Norbert.Nemec gmx.de> Jul 15 2004
- "Matthew Wilson" <admin.hat stlsoft.dot.org> Jul 16 2004
- Andy Friesen <andy ikagames.com> Jul 16 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
Jul 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
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
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
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
"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
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
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









"Matthew Wilson" <admin.hat stlsoft.dot.org> 