嵌套宏调用

mas*_*oud 4 c macros c99 c-preprocessor

尝试嵌套宏调用,如下所示:

#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;
}
Run Code Online (Sandbox Code Playgroud)

它产生错误:

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;
     ^~~
Run Code Online (Sandbox Code Playgroud)

有什么解决方法LET(s, INT) = 10;吗?

Sto*_*ica 6

这是两个空的

#define INT
#define LONG
Run Code Online (Sandbox Code Playgroud)

INT传递给AS(t)via 时,将LET进行中间扩展,并在扩展AS()自身之前将其扩展为空令牌序列。这样,您将连接AS_一个空的令牌序列。只需删除这两个宏,对于您的示例,具有AS_INTAS_LONG定义就足够了。