嵌套宏调用
Nested macro calls
正在尝试如下嵌套宏调用:
#include <stdint.h>
#define INT
#define LONG
#define AS(t) AS_##t
#define AS_INT as_int
#define AS_LONG as_long
#define LET(v, t) v. AS(t)
typedef union
{
int32_t as_int;
int64_t as_long;
} mytype_t;
int main()
{
mytype_t s;
s.AS(INT) = 10; /* This is OK */
LET(s, INT) = 10; /* This makes error */
return 0;
}
出现错误:
main.c:xx:yy: error: ‘mytype_t {aka union <anonymous>}’ has no member named ‘AS_’
#define LET(v, t) v. AS(t)
^
main.c:zz:ww: note: in expansion of macro ‘LET’
LET(s, INT) = 10;
^~~
是否有任何解决方法 LET(s, INT) = 10;
?
就是这两个空宏
#define INT
#define LONG
INT
当通过 LET
传递到 AS(t)
时进行中间扩展,并在 AS()
本身扩展之前扩展为空标记序列。因此,您将 AS_
与空标记序列连接起来。只需删除这两个宏,为您的示例定义 AS_INT
和 AS_LONG
就足够了。
已回答错误是由于空宏。
另一种解决方案可以是:
#define INT INT
#define LONG LONG
希望对您有所帮助。
正在尝试如下嵌套宏调用:
#include <stdint.h>
#define INT
#define LONG
#define AS(t) AS_##t
#define AS_INT as_int
#define AS_LONG as_long
#define LET(v, t) v. AS(t)
typedef union
{
int32_t as_int;
int64_t as_long;
} mytype_t;
int main()
{
mytype_t s;
s.AS(INT) = 10; /* This is OK */
LET(s, INT) = 10; /* This makes error */
return 0;
}
出现错误:
main.c:xx:yy: error: ‘mytype_t {aka union <anonymous>}’ has no member named ‘AS_’
#define LET(v, t) v. AS(t)
^
main.c:zz:ww: note: in expansion of macro ‘LET’
LET(s, INT) = 10;
^~~
是否有任何解决方法 LET(s, INT) = 10;
?
就是这两个空宏
#define INT
#define LONG
INT
当通过 LET
传递到 AS(t)
时进行中间扩展,并在 AS()
本身扩展之前扩展为空标记序列。因此,您将 AS_
与空标记序列连接起来。只需删除这两个宏,为您的示例定义 AS_INT
和 AS_LONG
就足够了。
已回答错误是由于空宏。
另一种解决方案可以是:
#define INT INT
#define LONG LONG
希望对您有所帮助。