Simple Macro Function Pointer Trick...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Thomasson

    #1

    Simple Macro Function Pointer Trick...

    I was wondering if the following technique will produce undefined behavior:
    _______________
    #include <cstdio>

    #define CALL_MACRO_FUNC TION(func_ptr)f unc_ptr()

    #define MY_MESSAGE() "Press <ENTERto exit."

    int main(void) {
    puts(CALL_MACRO _FUNCTION(MY_ME SSAGE));
    getchar();
    return 0;
    }

    _______________


    I define 'MY_MESSAGE' as a macro function and only pass the name of it to
    'CALL_MACRO_FUN CTION' which in turn uses the name to actually invoke it. The
    name of a macro function is analogous to a function pointer. This is useful
    when you need to have precise control over when a macro will actually
    expand.


    --
    Chris M. Thomasson


  • Chris Thomasson

    #2
    Re: Simple Macro Function Pointer Trick...

    "James Kanze" <james.kanze@gm ail.comwrote in message
    news:1189845690 .693508.110400@ n39g2000hsh.goo glegroups.com.. .
    On Sep 14, 4:16 pm, "Chris Thomasson" <cris...@comcas t.netwrote:
    >I was wondering if the following technique will produce undefined
    >behavior:
    >______________ _
    >#include <cstdio>
    [...]
    >______________ _
    Not related to your actual question, but the above shouldn't
    compile with a conformant implementation of <cstdio>.
    [...]

    DOH! Ah crap. Of course your correct. Here is some better code:

    _______________ _
    #include <cstdio>

    #define CALL_MACRO_FUNC TION(func_ptr)f unc_ptr()

    #define MY_MESSAGE() "Press <ENTERto exit."

    int main(void) {
    {
    using namespace std;
    puts(CALL_MACRO _FUNCTION(MY_ME SSAGE));
    getchar();
    }

    return 0;
    }

    _______________ _

    >I define 'MY_MESSAGE' as a macro function and only pass the
    >name of it to 'CALL_MACRO_FUN CTION' which in turn uses the
    >name to actually invoke it. The name of a macro function is
    >analogous to a function pointer. This is useful when you need
    >to have precise control over when a macro will actually
    >expand.
    It should work.
    Yeah. I agree. BTW, thank you for you response.

    Comment

    Working...