473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple Macro Function Pointer Trick...

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 <ENTERto 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.
--
Chris M. Thomasson
http://appcore.home.comcast.net

Sep 14 '07 #1
1 7509
"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@n39g2000hsh.googlegr oups.com...
On Sep 14, 4:16 pm, "Chris Thomasson" <cris...@comcast.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_FUNCTION(func_ptr)func_ptr()

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

int main(void) {
{
using namespace std;
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.
It should work.
Yeah. I agree. BTW, thank you for you response.

Sep 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment?...
3
by: Andrew | last post by:
Hello, Is it bad practice to use a function as a parameter of a macro? Will the compiler create a full copy of the function's machine code for each invocation of the macro? Or does it create...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
14
by: Michael B Allen | last post by:
I just noticed that doing something like the following may fail because it can overwrite u->size before it's evaluated. memcpy(u, buf, u->size); Is it legit that this is a macro as opposed to...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
9
by: jedi200581 | last post by:
Hello, I have a singleton class that I'm using through macros: //snippet #define DO_SOMETHING ( myArgument) \ { \ MyClass *myObject = MyClass::getInstance(); \...
45
by: madhawi | last post by:
Is it better to use a macro or a function?
12
by: swellfr | last post by:
Hi if have simple macro defined this way: #define M_OPplus( classname ) typedef classname operator+(const classname& rhs); and a template class : template<typename X, typename Y> class...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.