preprocessor optional / default function argument

P

Philipp Kraus

Hello,

I need a little bit help with this problem:
I would like to create a preprocessor function FUNC with an optional argument:

#define FUNC callme(__FUNCTION__);
#define FUNC(name) callme(name);

callme is a C++ function, so if the user is run

FUNC
the parameter of callme should be the function name and
if the user call
FUNC("test")
the function callme should get the parameter "test".

How can I do this with no and one argument?

Thanks a lot

Phil
 
V

Victor Bazarov

Hello,

I need a little bit help with this problem:
I would like to create a preprocessor function FUNC with an optional
argument:

#define FUNC callme(__FUNCTION__);
#define FUNC(name) callme(name);

callme is a C++ function, so if the user is run

FUNC
the parameter of callme should be the function name and
if the user call
FUNC("test")
the function callme should get the parameter "test".

How can I do this with no and one argument?

There is probably a solution for this more elegant, but why not have two
separate macros?

#define FUNCFUNC callme(__FUNCTION__)
#define FUNC(str) callme(str)

Since the user is going to differenciate them anyway, four characters
are not going to make the weather:

...
FUNCFUNC; // for a call with __FUNCTION__

...
FUNC("test"); // for a call with "test"

.. And I strongly recommend not to include the semicolon as part of the
macro. That way you could use the macro where an expression is expected.

V
 

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,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top