Overloading a variadic macro

This is a discussion on Overloading a variadic macro within the C Programming forums, part of the General Programming Boards category; Hi. I'm using gcc with -std=c99. I want to do this: #define myfunction (A,B) myfunction_ (__FILE__,__LINE__ ,A,B) #define myfunction (A,B,C,...) ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Overloading a variadic macro

    Hi. I'm using gcc with -std=c99. I want to do this:

    #define myfunction (A,B) myfunction_ (__FILE__,__LINE__ ,A,B)
    #define myfunction (A,B,C,...) myfunctionf_ (__FILE__,__LINE__,A,B,C,__VAR_ARGS__)

    The idea is that I can call myfunction with a variable number of arguments. If there are exactly two, myfunction_ is given control. If there are three or more, myfunctionf_ is called. Is this possible in C at all?

    Richard

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    Hi. I'm using gcc with -std=c99. I want to do this:

    #define myfunction (A,B) myfunction_ (__FILE__,__LINE__ ,A,B)
    #define myfunction (A,B,C,...) myfunctionf_ (__FILE__,__LINE__,A,B,C,__VAR_ARGS__)

    The idea is that I can call myfunction with a variable number of arguments. If there are exactly two, myfunction_ is given control. If there are three or more, myfunctionf_ is called. Is this possible in C at all?

    Richard
    c99 does not support function overloading.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    126
    Quote Originally Posted by CommonTater View Post
    c99 does not support function overloading.
    Not even for a variadic macro? It's not really a function.

    I guess we could do something like this, but it's not Standard C, which I want.

    Code:
    #define myfunction (A,__VA_ARGS__) 
    { 
    #if __VA__NARG==2
    myfunction_ ( A,__VA_ARGS__)
    #elseif
    myfunctionf_ (A,__VA_ARGS__)
    #endif
    }
    Last edited by Richardcavell; 03-14-2011 at 10:21 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    Not even for a variadic macro? It's not really a function.

    I guess we could do something like this, but it's not Standard C, which I want.

    Code:
    #define myfunction (A,__VA_ARGS__) 
    { 
    #if __VA__NARG==2
    myfunction_ ( A,__VA_ARGS__)
    #elseif
    myfunctionf_ (A,__VA_ARGS__)
    #endif
    }
    What exactly are you trying to accomplish?

    Most times I find these horrible kludges, are trying to do something fairly simple in the hardest way possible....

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Error checking

    Quote Originally Posted by CommonTater View Post
    What exactly are you trying to accomplish?

    Most times I find these horrible kludges, are trying to do something fairly simple in the hardest way possible....
    It goes like this: I am passing a string as a parameter. If there are no further arguments, it is printed to screen with printf ("%s", pstring ) ;, and similarly to log file.

    If there are further arguments, it gets done with: printf ( pstring, ... )

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Google for ppnarg. pp_narg macro - Google Search
    Using this macro and concat you can now have macro overloading depending on no of args.
    http://stackoverflow.com/questions/3...n-pasting-in-c
    Last edited by Bayint Naung; 03-14-2011 at 07:42 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you define multiple macros with the same name, you only get the one that's closest to you.
    Code:
    #define THIS wasreplacedby
    #define THIS onehere

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  2. Macro problem
    By TriKri in forum C Programming
    Replies: 6
    Last Post: 05-14-2010, 03:52 AM
  3. Replies: 2
    Last Post: 04-12-2010, 01:57 PM
  4. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  5. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21