Suppressing "ISO C99 requires rest arguments to be used"

11,810

Solution 1

Combine the s argument with the variadic arguments so that you always have at least one argument as part of the ellipsis. This also allows you to avoid using the ,## extension of GCC:

#define PNORM( v, ... )  { \
  if( VERBOSITY_CHECK( v ) ) { \
    if( ( errno = pthread_mutex_lock(&server.output_mutex) ) ) { \
      PERROR_LOCKFREE( normal, "\tpthread_mutex_lock failed on output_mutex.\r\n" ) ; \
    } \
    fprintf( stdout, __VA_ARGS__ ) ; \
    fflush( stdout ) ; \
    if( ( errno = pthread_mutex_unlock(&server.output_mutex) ) ) { \
      PERROR_LOCKFREE( normal, "\tpthread_mutex_unlock failed on output_mutex.\r\n" ) ; \
    } \
  } \
}

#define PERROR_LOCKFREE( v, ... ) { \
  if( VERBOSITY_CHECK( v ) ) { \
    PERRNO ;\
    fprintf( stderr, __VA_ARGS__ ) ; \
    fflush( stderr ) ; \
  } \
}

Solution 2

The ## token in combination with __VA_ARGS__ is a gcc extension that's not part of ISO C99. That's why you're getting the warning.

Solution 3

You can disable warnings just around your macros, or disable the specific warning entirely with pragma Warnings in GCC. You could also not use -pedantic, since it is, well, pedantic.

Solution 4

Depends on what is simple for you. In P99 there are P99 conditionals that would allow you doing something like

#define USER_MACRO(...) P99_IF_DEC_LE(P99_NARG(__VA_ARGS__),2)(USER_MACRO2(__VA_ARGS__))(USER_MACRO3(__VA_ARGS__))

So with that there is no need for the ,## extension of gcc.

Share:
11,810
David Mokon Bond
Author by

David Mokon Bond

David Bond is an innovator, technologist, and software engineer with expertise in C/C++, SDN, NFV, routing algorithms, and networking. Feel free to follow his blog and twitter account. He is currently working for an undisclosed company. Before this he was a Staff Software Engineer at Vyatta, a Brocade Company, working on the Linux based next generation Brocade Vyatta 5400/5600 vRouter focusing on the areas of NFV and routing (BGP, PIM, OSPF, IS-IS, etc) (C/Perl/Linux) and representing Brocade at the IETF. Prior to that he was a Staff Software Engineer at IBM System Networking (formerly Blade Networks) working as the TRILL subject matter expert implementing a TRILL solution. He was also an active participant in the IETF TRILL working group having co-authored the current TRILL OAM requirements RFC and participated as a member in the TRILL OAM design team. Previously he was a research and development software engineer and the Layer 2 technical manager in the University of New Hampshire InterOperability Laboratory’s (UNH-IOL) Bridge Functions Consortium. In this role, David handled research and development on in-house tools, business development, and strategic planning. He developed the TRILL testing program at the lab. David graduated summa cum laude with a B.S. in Computer Science from the University of New Hampshire in 2009. He has taken graduate work towards a Ph.D. in Computer Science at the University of New Hampshire.

Updated on June 04, 2022

Comments

  • David Mokon Bond
    David Mokon Bond almost 2 years

    Consider the following two macros:

    #define PNORM( v, s, ... )  { \
      if( VERBOSITY_CHECK( v ) ) { \
        if( ( errno = pthread_mutex_lock(&server.output_mutex) ) ) { \
          PERROR_LOCKFREE( normal, "\tpthread_mutex_lock failed on output_mutex.\r\n" ) ; \
        } \
        fprintf( stdout, s, ## __VA_ARGS__ ) ; \
        fflush( stdout ) ; \
        if( ( errno = pthread_mutex_unlock(&server.output_mutex) ) ) { \
          PERROR_LOCKFREE( normal, "\tpthread_mutex_unlock failed on output_mutex.\r\n" ) ; \
        } \
      } \
    }
    
    #define PERROR_LOCKFREE( v, s, ... ) { \
      if( VERBOSITY_CHECK( v ) ) { \
        PERRNO ;\
        fprintf( stderr, s, ## __VA_ARGS__ ) ; \
        fflush( stderr ) ; \
      } \
    }
    

    Now consider an example use of these:

    PNORM( verbose, "\tSomeText [%d] More [%p]\r\n", 0, ptr ) ;
    

    When compiled with the -pedantic option and -std=c99 I get this error many times:

    mycode.c:410:112: warning: ISO C99 requires rest arguments to be used
    

    The complier is right in complaining about this but is there a simple way I can suppress this warning since I don't care about it?