【问题标题】:Suppressing "ISO C99 requires rest arguments to be used"禁止“ISO C99 需要使用其他参数”
【发布时间】:2010-11-04 19:58:17
【问题描述】:

考虑以下两个宏:

#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 ) ; \
  } \
}

现在考虑使用这些的示例:

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

使用 -pedantic 选项和 -std=c99 编译时,我多次收到此错误:

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

编译器对此的抱怨是正确的,但有没有一种简单的方法可以抑制这个警告,因为我不在乎它?

【问题讨论】:

    标签: c gcc posix variadic-functions gcc-warning


    【解决方案1】:

    s 参数与可变参数组合在一起,这样您就始终至少有一个参数作为省略号的一部分。这也可以让您避免使用 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 ) ; \
      } \
    }
    

    【讨论】:

      【解决方案2】:

      ## 令牌与__VA_ARGS__ 结合使用是不属于 ISO C99 的 gcc 扩展。这就是您收到警告的原因。

      【讨论】:

        【解决方案3】:

        您可以在宏周围使用disable 警告,或者在 GCC 中使用pragma Warnings 完全禁用特定警告。你也不能使用-pedantic,因为它很迂腐。

        【讨论】:

        • Pedantic 是一个非常好的功能,可用于帮助捕获代码中的小错误。警告不容忽视。
        • @David:当然,但问题是“我如何忽略这个警告。” -pedantic 实际上只对捕获 gcc 依赖项有用。 -Wall 将捕获几乎所有可以通过警告捕获的错误。
        • 这个答案真的无助于禁用那个警告。此外,至少我的 gcc 版本不支持pragma Warnings
        【解决方案4】:

        取决于对你来说什么是简单的。在 P99 中有 P99 conditionals 可以让你做类似的事情

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

        因此,不需要 gcc 的 ,## 扩展。

        【讨论】:

        • 这应该是公认的答案。更优雅。
        猜你喜欢
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        • 2020-08-24
        • 1970-01-01
        • 2012-05-01
        • 2016-07-26
        • 2017-10-14
        • 2021-01-03
        相关资源
        最近更新 更多