抑制“ISO C99 需要使用剩余参数”
考虑以下两个宏:
#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
编译器对此的抱怨是正确的,但有没有一种简单的方法可以抑制这个警告因为我不关心它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
s
参数与可变参数相结合,以便始终至少有一个参数作为省略号的一部分。这还允许您避免使用 GCC 的,##
扩展: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:##
标记与__VA_ARGS__
组合是一个 gcc 扩展,不属于 ISO C99。这就是您收到警告的原因。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.您可以禁用宏周围的警告,或完全禁用特定警告在 GCC 中使用
pragma warnings
。你也不能使用-pedantic
,因为它确实是迂腐的。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.取决于什么对你来说是简单的。在 P99 中有 P99 条件 可以让你做类似
So 的 事情不需要 gcc 的
,##
扩展。Depends on what is simple for you. In P99 there are P99 conditionals that would allow you doing something like
So with that there is no need for the
,##
extension of gcc.