Simple Macro Function Pointer Trick...

C

Chris Thomasson

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

#define CALL_MACRO_FUNCTION(func_ptr)func_ptr()

#define MY_MESSAGE() "Press <ENTER> to exit."

int main(void) {
puts(CALL_MACRO_FUNCTION(MY_MESSAGE));
getchar();
return 0;
}

_______________


I define 'MY_MESSAGE' as a macro function and only pass the name of it to
'CALL_MACRO_FUNCTION' 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.
 
J

James Kanze

I was wondering if the following technique will produce undefined behavior:
_______________
#include <cstdio>
#define CALL_MACRO_FUNCTION(func_ptr)func_ptr()
#define MY_MESSAGE() "Press <ENTER> to exit."
int main(void) {
puts(CALL_MACRO_FUNCTION(MY_MESSAGE));
getchar();
return 0;
}
_______________

Not related to your actual question, but the above shouldn't
compile with a conformant implementation of <cstdio>. puts and
getchar should be in std, and no where else. (I don't think any
of the implementations are conformant in this regard. But
unless said:
I define 'MY_MESSAGE' as a macro function and only pass the
name of it to 'CALL_MACRO_FUNCTION' 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. When expanding a macro, the preprocessor
rescans the substituted text for further expansions, and it only
recognizes a function style macro if the name is immediately
followed by an opening parentheses. (Thus, for example, in C,
getchar could be a function style macro. But in expressions
like:
int (* pf)() = &getchar ;
or
(getchar)() ;
the macro is not recognized, and the actual function which it
hides is used.)
 
C

Chris Thomasson

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_FUNCTION(func_ptr)func_ptr()

#define MY_MESSAGE() "Press <ENTER> to exit."

int main(void) {
{
using namespace std;
puts(CALL_MACRO_FUNCTION(MY_MESSAGE));
getchar();
}

return 0;
}

________________

It should work.

Yeah. I agree. BTW, thank you for you response.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top