Calling function depending upon MACRO value

P

param

Hi,

I need to write a MACRO which helps to call different functions
depending upon it's value say On or OFF. If its' value is ON the
function1 should get called and if value is OFF function2 gets called .

It's like same as assert gets executed when NDEBUG is not defined at
the start of code. If NDEBUG is defined assert doesn't gets called at
all.

Thanks
dhesi
 
B

Ben Pope

param said:
Hi,

I need to write a MACRO

Are you sure?
which helps to call different functions
depending upon it's value say On or OFF. If its' value is ON the
function1 should get called and if value is OFF function2 gets called .

If you must use a macro, hide it's details behind a function call if
possible, making the macro merely an implementation detail.
It's like same as assert gets executed when NDEBUG is not defined at
the start of code. If NDEBUG is defined assert doesn't gets called at
all.

Can you provide an example of the functions you need to call?

Ben Pope
 
J

Jim Langston

param said:
Hi,

I need to write a MACRO which helps to call different functions
depending upon it's value say On or OFF. If its' value is ON the
function1 should get called and if value is OFF function2 gets called .

It's like same as assert gets executed when NDEBUG is not defined at
the start of code. If NDEBUG is defined assert doesn't gets called at
all.

Thanks
dhesi

#define RUNTEST

#ifdef RUNTEST
#define Func(x) MyFunc(x)
#else
#define Func(x) {}
#endif

Truth be told, however, you should avoid uses of macros whenever possible.
 
I

Ivan Vecerina

: : > Hi,
: >
: > I need to write a MACRO which helps to call different functions
: > depending upon it's value say On or OFF. If its' value is ON the
: > function1 should get called and if value is OFF function2 gets called
..
: >
: > It's like same as assert gets executed when NDEBUG is not defined at
: > the start of code. If NDEBUG is defined assert doesn't gets called at
: > all.
: >
: > Thanks
: > dhesi
:
: #define RUNTEST
:
: #ifdef RUNTEST
: #define Func(x) MyFunc(x)
: #else
: #define Func(x) {}

The latter won't work properly with code such as:
if(test)
Func();
else
OtherStuff();
A classic 'no-op' function macro substitution is:
#define Func(x) ((void)0)
[ or the more general: do{ /*whatever*/ }while(0) ]

: #endif

:
: Truth be told, however, you should avoid uses of macros whenever
possible.

Yes!


Regards,
Ivan
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top