c++ - 如何使用可变参数宏?
问题描述
我正在尝试使用可变参数宏。我希望从 MY_TOP_PRINTF 宏中调用 PRINTF 宏。
1.要么我得到编译错误。
2.如果我删除编译错误,它只打印第一个参数。
我的预期结果是Inside TOP_PRINT dog here 4
#include <iostream>
#include <cstdio>
using namespace std;
#define PRINTF(str, ...) { \
fprintf(stderr, (str), ##__VA_ARGS__); \
}
#define MY_TOP_PRINTF(EXPRESSION, ...) { \
PRINTF("Inside TOP_PRINT ", EXPRESSION, __VA_ARGS__);\
}
int main()
{
int x = 4;
char str[255] = "dog here";
MY_TOP_PRINTF(str,x);
return 0;
}
错误:
hello_temp.cpp: In function ‘int main()’:
hello_temp.cpp:8:41: warning: too many arguments for format [-Wformat-extra-args]
fprintf(stderr, (str), ##__VA_ARGS__); \
^
hello_temp.cpp:13:5: note: in expansion of macro ‘PRINTF’
PRINTF("Inside TOP_PRINT", EXPRESSION, __VA_ARGS__);\
^
hello_temp.cpp:23:2: note: in expansion of macro ‘MY_TOP_PRINTF’
MY_TOP_PRINTF(str,x);
解决方案
推荐阅读
- angular - 在 Angular 中,在声明路由时使用 array.map() 会导致“无法读取未定义的属性‘loadChildren’时出现错误”
- android - 回收站视图未在活动中显示任何内容
- c++ - 有没有办法调用函数的多个继承覆盖?
- .net - 如何在每个字段中使用读写器锁
- javascript - 我们如何以不同的格式显示时区?
- asp.net-core - 通过 AuthorizationHandler 访问绑定模型
- primefaces - Primefaces 7.0 旧库的替代品?
- javascript - AngularJs $q.all 不适用于 for 循环
- django - 如何修复关键错误:在 django 中使用 woocommerce api 时出现“x-wp-totalpages”
- java - 使用 Jacoco 创建名为“liquibase”的 bean 时出错