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.
_______________
#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.