Thread: overloading macros?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    overloading macros?

    can it be done?


    Ex:
    getstring(a) ...
    getstring(a, b) ...
    getstring(a, b, c) ...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    By macro I'm assuming you mean something like
    #define getline(a) //code here

    The first thing that came to mind when I read this was why would you want to? Since you can overload functions and inlining functions achieves the same effect as macros there's no need to even write macros, much less overload them.

    As for whether you can or not, give it a try.

    -Prelude
    My best code is written with the delete key.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    From my understanding, you should try avoid macros where possible.......They dont always behave as they should and they have no type safety.

    Try const functions

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >
    can it be done?

    Ex:
    getstring(a) ...
    getstring(a, b) ...
    getstring(a, b, c) ...
    <

    No, you just be using the last one defined.

    eg. if you called getstring("whatever");
    you compiler would say ", expected" where its expecting a comma between arguements.

    >
    Since you can overload functions and inlining functions achieves the same effect as macros there's no need to even write macros,
    <

    there's effiency reasons since functions are called at runtime and macros evaluated during preprocessng.

    >
    From my understanding, you should try avoid macros where possible.......They dont always behave as they should and they have no type safety.
    <

    with careful use they can be quite handy,
    Last edited by no-one; 01-24-2002 at 01:05 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >there's effiency reasons since functions are called at runtime

    inline functions aren't called at runtime. There's no guarantee that a compiler will inline it but you'd have to have a pretty dumb compiler that wouldn't inline something simple that you'd normally write a macro for.

  6. #6
    Unregistered
    Guest
    Macros are not called so there is no stack frame overhead. The compiler substitutes word for word your code within the macro when you use it, then compiles the final product.

    Macros can be very useful when used correctly but can be very confusing when used too much.

  7. #7
    Unregistered
    Guest
    >
    There's no guarantee that a compiler will inline
    <

    the macro is guaranteed to work the way you'd expect, and the function has type checking.

    use functions where functions belong and macros where they are needed. its user preference.

    >
    it but you'd have to have a pretty dumb compiler that wouldn't inline something simple that you'd normally write a macro for.
    <

    i don't know i've seen some preety complex macros, that i doubt would be inlined or be used effectively as a function.

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Macros are not called so there is no stack frame overhead. The compiler substitutes word for word your code within the macro when you use it, then compiles the final product.

    This is exactly what happens with inline functions, but you get the added benefit of compile time type checking.

    >use functions where functions belong and macros where they are needed.

    Why?

    >i don't know i've seen some preety complex macros, that i doubt would be inlined or be used effectively as a function.

    An example? Besides if you're writing long and complex macros that are being frequently substituted in your code then you're just going to produce code bloat, which will negate the efficiency benefit you've gained from eliminating the function call.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    macros are not checked as thoroughly as regular functions.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Also, try passing an expression like 'x++' to a macro, then pass it to a function which does the same thing -- unless the macro was written specifically to avoid this, you'll come up, many times, with the wrong answer. A function, even an inline one, cannot modify the parameters unless they're passed by reference -- a macro most certainly can, and if badly written, will, modify this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  3. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  4. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  5. operator overloading
    By ihsir in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2002, 04:38 PM