preprocessor optional / default function argument

Discussion in 'C++' started by Philipp Kraus, Jun 2, 2013.

  1. 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
    Philipp Kraus, Jun 2, 2013
    #1
    1. Advertising

  2. On 6/2/2013 1:45 PM, Philipp Kraus wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 3, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cronus
    Replies:
    1
    Views:
    667
    Paul Mensonides
    Jul 15, 2004
  2. Rudy Moore
    Replies:
    2
    Views:
    438
    Sumit Rajan
    Sep 24, 2004
  3. Reckoner
    Replies:
    11
    Views:
    699
    Steven D'Aprano
    Jan 19, 2009
  4. Pushkar Prasad

    How to declare a function argument as optional?

    Pushkar Prasad, Feb 7, 2011, in forum: C Programming
    Replies:
    16
    Views:
    1,241
    Joel C. Salomon
    Feb 7, 2011
  5. AikidoGuy
    Replies:
    11
    Views:
    558
    Seebs
    Nov 21, 2011
Loading...

Share This Page