c - 使可变长度输入参数无效的 C 宏
问题描述
有没有办法定义一个使变量变量列表无效的宏?
#define VOID_ARGS(...) ((void)##__VA_ARGS__)
当警告被视为错误时,用例是用于抑制编译器错误 [-Werror=unused-value] 的 void 参数:
#define DEBUG 1
#ifdef DEBUG
#define func(fmt, ...) dbg_func(fmt, ##__VA_ARGS__)
#else
#define func(fmt, ...) VOID_ARGS(fmt, ##__VA_ARGS__)
#endif
解决方案
这是否让您知道如何解决该问题?
调试.h:
extern int dbg_func(const char *format, ...);
//Does nothing
extern int ignoreDebug(const char *format, ...);
#define DEBUG 1
#ifdef DEBUG
#define func(fmt, ...) dbg_func(fmt, ##__VA_ARGS__)
#else
#define func(fmt, ...) ignoreDebug(fmt, ##__VA_ARGS__)
#endif
调试.c:
int ignoreDebug(const char *format, ...)
{
(void)format;
return 0;
}
int dbg_func(const char *format, ...)
{
TODO: Some code needs to go here.
return 0;
}
推荐阅读
- c++ - 使用管道在 C++ 中创建调度队列/线程处理程序:FIFO 溢出
- javascript - 使用 javascript 在悬停时播放视频
- android - Android 9 (Pie) - 无法通过 Charles proxy 和 Fiddler 等网络监控工具捕获任何 api (native / web)
- html - CSS 网格重新创建
- c# - 每当我拉下我的 ASP.NET Web API 存储库时,VSCode intellisense 都会报告错误,为什么?
- bit - 一个字节中带有位选项标志的 7 位循环缓冲区
- python - 在python中猴子修补类的语法
- azure - 将 SSIS 提升并转移到 Azure SQL DB(连接管理器错误)
- windows - Get-ChildItem 尝试扫描网络以查找所有 .mdb 文件时出错
- python - Python:用换行符打印 ujson