difference bet. macro and inline

Discussion in 'C++' started by sachin_mzn@yahoo.com, Dec 28, 2004.

  1. Guest

    Hi,

    It may be a silly question but I want to know
    the difference between #define macro and inline functions
    Is there any performance issue related to it.

    -Sachin
     
    , Dec 28, 2004
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > It may be a silly question but I want to know
    > the difference between #define macro and inline functions
    > Is there any performance issue related to it.
    >
    > -Sachin
    >


    #define max(a,b) ((a>b) ? (a) : (b))

    inline template<class T> T max(const T& t1, const T& t2)
    {
    return t1>t2 ? t1 : t2;
    }

    will produce the same results on good compilers.


    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
     
    Gernot Frisch, Dec 28, 2004
    #2
    1. Advertising

  3. Mole Wang Guest

    "Gernot Frisch" <> wrote in message news:...
    >
    > <> schrieb im Newsbeitrag
    > news:...
    > > Hi,
    > >
    > > It may be a silly question but I want to know
    > > the difference between #define macro and inline functions
    > > Is there any performance issue related to it.
    > >
    > > -Sachin
    > >

    >
    > #define max(a,b) ((a>b) ? (a) : (b))
    >
    > inline template<class T> T max(const T& t1, const T& t2)
    > {
    > return t1>t2 ? t1 : t2;
    > }
    >
    > will produce the same results on good compilers.


    Really? Consider the following code:
    int i = 3, j = 2;
    int res = max(++i, j);
    The inline function and macro result in different return value "res".

    >
    >
    > --
    > -Gernot
    > int main(int argc, char** argv) {printf
    > ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
    >
    > ________________________________________
    > Looking for a good game? Do it yourself!
    > GLBasic - you can do
    > www.GLBasic.com
    >
    >
     
    Mole Wang, Dec 28, 2004
    #3
  4. BB Guest

    wrote:
    > Hi,
    >
    > It may be a silly question but I want to know
    > the difference between #define macro and inline functions
    > Is there any performance issue related to it.
    >
    > -Sachin
    >


    This is addressed in the FAQ.
     
    BB, Dec 28, 2004
    #4
  5. Ron Natalie Guest

    Gernot Frisch wrote:

    > #define max(a,b) ((a>b) ? (a) : (b))
    >
    > inline template<class T> T max(const T& t1, const T& t2)
    > {
    > return t1>t2 ? t1 : t2;
    > }
    >
    > will produce the same results on good compilers.
    >
    >


    No it will NOT.

    max(++a, ++b)

    will yield different results.
     
    Ron Natalie, Dec 28, 2004
    #5
  6. Gernot Frisch wrote:
    > #define max(a,b) ((a>b) ? (a) : (b))


    Two more parentheses, please:
    #define max(a,b) ((a)>(b) ? (a) : (b))

    (Try calling max(1, 1 & 2) to spot the difference!)

    > inline template<class T> T max(const T& t1, const T& t2)
    > {
    > return t1>t2 ? t1 : t2;
    > }
    >
    > will produce the same results on good compilers.


    Ron Natalie replied:
    >
    > No it will NOT.
    >
    > max(++a, ++b)
    >
    > will yield different results.



    max(a, b) = 0;

    will yield different results as well. Apparently, the macro approach is
    the better one, in this case!

    Scott Meyers wrote about the implementation of max(a, b), in his article
    "min, max, and more": <quote> I increasingly find myself telling people
    that the macro approach may well be best, and I hate macros. </quote>

    Source: http://www.aristeia.com/Papers/C ReportColumns/jan95.pdf


    Kind regards,

    Niels Dekker
    http://www.xs4all.nl/~nd/dekkerware
     
    Niels Dekker - no reply address, Dec 29, 2004
    #6
  7. Ron Natalie Guest

    Niels Dekker - no reply address wrote:

    > max(a, b) = 0;


    add:
    inline template<class T> T& max(T& t1, T& t2)
    {
    return t1 > t2 ? t1 : t2;
    }

    >
    > will yield different results as well. Apparently, the macro approach is
    > the better one, in this case!
    >
    > Scott Meyers wrote about the implementation of max(a, b), in his article
    > "min, max, and more": <quote> I increasingly find myself telling people
    > that the macro approach may well be best, and I hate macros. </quote>
    >
     
    Ron Natalie, Dec 29, 2004
    #7
  8. Gernot Frisch wrote:
    > inline template<class T> T max(const T& t1, const T& t2)
    > {
    > return t1>t2 ? t1 : t2;
    > }


    To support assignment [max(a, b) = 0], Ron Natalie wrote:
    >
    > add:
    > inline template<class T> T& max(T& t1, T& t2)
    > {
    > return t1 > t2 ? t1 : t2;
    > }


    The Scott Meyers article "min, max, and more" (1995) said that this
    would still lead to troubles when mixing const and non-const arguments:

    void g(const BigNumber& n1)
    {
    BigNumber n2 = 22;
    BigNumber n3 = max(n1, n2); // call which max?...
    }

    (From http://www.aristeia.com/Papers/C ReportColumns/jan95.pdf again)
    But all of the compilers I just tried accept the code. So may I assume
    that this issue has been solved by a revision of the C++ language?


    Kind regards,

    Niels Dekker
    http://www.xs4all.nl/~nd/dekkerware
     
    Niels Dekker - no reply address, Dec 29, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sham Ramakrishnan

    SOAP remote method calls? the best bet?

    Sham Ramakrishnan, Jul 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Sham Ramakrishnan
    Jul 2, 2003
  2. Daniel Bass
    Replies:
    2
    Views:
    3,780
    dave wanta
    Jul 4, 2003
  3. Raja Balaji R

    information bet. aspnet pages

    Raja Balaji R, Jan 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    331
    Kevin Spencer
    Jan 9, 2004
  4. =?Utf-8?B?RmF0Ym95Y2FudGVlbg==?=

    How to add dynamic event(asp.bet)

    =?Utf-8?B?RmF0Ym95Y2FudGVlbg==?=, Jan 21, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,103
    Marshal Antony
    Jan 21, 2004
  5. slyphiad

    Friend Class Blackjack and Bet

    slyphiad, Oct 13, 2004, in forum: C++
    Replies:
    3
    Views:
    421
    John Harrison
    Oct 15, 2004
Loading...

Share This Page