使用 ## 运算符扩展宏
Macros expansion using ## operator
大家好,问题是我在这个宏中遇到的问题
#define ADD_COMP(s1,s2,type)({\
int _x=0;\
for(int i=0;i<n_addrs;i++){\
if(memcmp(s1,&(s2->##type),6)!=0){\
_x=-1;\
}else{\
break;\
}\
}\
_x;\
})
s1 是一个简单的数组,s2 是一个有 4 个向量作为成员的结构,像这样
typedef struct example{
char[6] one,
char[6] two,
char[6] three
}example;
现在出于自己的原因,我需要创建一个函数来比较大小为 6 字节的 s1 数组和示例中的一个成员,因此为此我使用 ## 运算符编写了 ADD_CMP 以使其更通用尽可能
所以我定义:
#define one
#define two
#define three
我以这种方式多次使用该函数,希望宏扩展成功
ADD_COMP(some_array,example1,one)
ADD_COMP(some_array,example1,two)
ADD_COMP(some_array,example1,three)
但编译器 return 为错误:
error: pasting "->" and "one" does not give a valid preprocessing token
error: pasting "->" and "two" does not give a valid preprocessing token
error: pasting "->" and "three" does not give a valid preprocessing token
如何在不为每个结构成员编写相同函数的情况下修复它?
如错误所示,从来不需要 ##
,因为它用于粘贴两个预处理标记以形成一个。
#define VAR(name, num) name##num
int VAR(foo, 1); // foo and 1 must be pasted together as foo1, instead of foo 1
编译宏时应修复多个语法错误和缺失的声明
#include<stdlib.h>
#include<string.h>
int n_addrs = 6;
#define ADD_COMP(s1,s2,type) {\
int _x=0;\
for(int i=0;i<n_addrs;i++){\
if(memcmp(s1,&(s2->type),6)!=0){\
_x=-1;\
}else{\
break;\
}\
}\
_x;\
}
typedef struct example{
char one[6];
char two[6];
char three[6];
}example;
void foo(void)
{
example* example1 = malloc(sizeof(example));
char some_array[6];
ADD_COMP(some_array,example1,one)
ADD_COMP(some_array,example1,two)
ADD_COMP(some_array,example1,three)
}
注意一个 compound statement { ... }
isn't an expression and can not be used as such. By adding extra parentheses around it, you are using gnu's extension 并且不是标准的 C。
相反,您应该编写一个函数来执行此操作。然后您将能够 return _x
并且使用现代优化器,应该可以忽略不计的开销。
我不确定您为什么要尝试使用 ##
预处理器令牌。
宏可以简单地是:
#define ADD_COMP(s1, s2, member) ({\
int _x = 0;\
for (int i = 0; i < n_addrs; i++){\
if(memcmp(s1, &(s2.member), 6) !=0 ){\
_x = -1;\
} else {\
break;\
}\
}\
_x;\
})
并用作:
ADD_COMP(some_array,example1,one);
ADD_COMP(some_array,example1,two);
ADD_COMP(some_array,example1,three);
更重要的是,宏看起来很简单,可以用函数代替,这总是更好的选择。我无法建议如何将宏转换为函数,因为我不清楚 n_addrs
的来源以及 _x
的使用位置和方式。
令牌粘贴运算符专为您想要将两个不同的预处理器令牌粘合到一个令牌中的情况而设计。例如,您可以写类似
#define Glue(x) x my##x_
然后写
Glue(int);
获取此变量声明:
int my_int;
在这里,令牌粘贴将令牌“my_”与令牌“int”组合在一起形成新令牌“my_int”,这是一个代表名称的新单个令牌。
一旦你将两个标记粘贴在一起,预处理器就不会重新扫描它们来确定它是否是几个不同的单个标记的组合。它将形成的任何东西都视为单个标记。例如,这段代码将无法编译:
#define BadAdd(x, y) x##+##y
int z = BadAdd(137, 42);
这里的问题是令牌粘贴形成了一个预处理令牌137+42
。预处理器然后尝试将此预处理标记映射到单个逻辑标记,但没有可以对应的单个标记。通常,C 或 C++ 会将其视为三个单独的标记(137、+ 和 42),但由于您强行将它们粘在一起,编译器不知道它在看什么。
将此与更传统的 Add 宏进行对比,后者出于说明的目的省略了大量重要的括号:
#define Add(x, y) x + y
int z = Add(137, 42);
此处,Add(137, 42) 扩展为三个标记 (137, +, 42) 的序列,随后编译器可以将其解释为加法表达式。
你上面写的宏就像BadAdd宏。通过将 -> 标记与字段名称粘合在一起,您最终会得到一个像 ->one
这样的单元,编译器无法将其有意义地解释为单个标记。只需删除此处的 ## - 就像从 BadAdd 到 Add 一样,这将生成一系列标记而不是单个标记,这正是您在这里想要的。
大家好,问题是我在这个宏中遇到的问题
#define ADD_COMP(s1,s2,type)({\
int _x=0;\
for(int i=0;i<n_addrs;i++){\
if(memcmp(s1,&(s2->##type),6)!=0){\
_x=-1;\
}else{\
break;\
}\
}\
_x;\
})
s1 是一个简单的数组,s2 是一个有 4 个向量作为成员的结构,像这样
typedef struct example{
char[6] one,
char[6] two,
char[6] three
}example;
现在出于自己的原因,我需要创建一个函数来比较大小为 6 字节的 s1 数组和示例中的一个成员,因此为此我使用 ## 运算符编写了 ADD_CMP 以使其更通用尽可能 所以我定义:
#define one
#define two
#define three
我以这种方式多次使用该函数,希望宏扩展成功
ADD_COMP(some_array,example1,one)
ADD_COMP(some_array,example1,two)
ADD_COMP(some_array,example1,three)
但编译器 return 为错误:
error: pasting "->" and "one" does not give a valid preprocessing token
error: pasting "->" and "two" does not give a valid preprocessing token
error: pasting "->" and "three" does not give a valid preprocessing token
如何在不为每个结构成员编写相同函数的情况下修复它?
如错误所示,从来不需要 ##
,因为它用于粘贴两个预处理标记以形成一个。
#define VAR(name, num) name##num
int VAR(foo, 1); // foo and 1 must be pasted together as foo1, instead of foo 1
编译宏时应修复多个语法错误和缺失的声明
#include<stdlib.h>
#include<string.h>
int n_addrs = 6;
#define ADD_COMP(s1,s2,type) {\
int _x=0;\
for(int i=0;i<n_addrs;i++){\
if(memcmp(s1,&(s2->type),6)!=0){\
_x=-1;\
}else{\
break;\
}\
}\
_x;\
}
typedef struct example{
char one[6];
char two[6];
char three[6];
}example;
void foo(void)
{
example* example1 = malloc(sizeof(example));
char some_array[6];
ADD_COMP(some_array,example1,one)
ADD_COMP(some_array,example1,two)
ADD_COMP(some_array,example1,three)
}
注意一个 compound statement { ... }
isn't an expression and can not be used as such. By adding extra parentheses around it, you are using gnu's extension 并且不是标准的 C。
相反,您应该编写一个函数来执行此操作。然后您将能够 return _x
并且使用现代优化器,应该可以忽略不计的开销。
我不确定您为什么要尝试使用 ##
预处理器令牌。
宏可以简单地是:
#define ADD_COMP(s1, s2, member) ({\
int _x = 0;\
for (int i = 0; i < n_addrs; i++){\
if(memcmp(s1, &(s2.member), 6) !=0 ){\
_x = -1;\
} else {\
break;\
}\
}\
_x;\
})
并用作:
ADD_COMP(some_array,example1,one);
ADD_COMP(some_array,example1,two);
ADD_COMP(some_array,example1,three);
更重要的是,宏看起来很简单,可以用函数代替,这总是更好的选择。我无法建议如何将宏转换为函数,因为我不清楚 n_addrs
的来源以及 _x
的使用位置和方式。
令牌粘贴运算符专为您想要将两个不同的预处理器令牌粘合到一个令牌中的情况而设计。例如,您可以写类似
#define Glue(x) x my##x_
然后写
Glue(int);
获取此变量声明:
int my_int;
在这里,令牌粘贴将令牌“my_”与令牌“int”组合在一起形成新令牌“my_int”,这是一个代表名称的新单个令牌。
一旦你将两个标记粘贴在一起,预处理器就不会重新扫描它们来确定它是否是几个不同的单个标记的组合。它将形成的任何东西都视为单个标记。例如,这段代码将无法编译:
#define BadAdd(x, y) x##+##y
int z = BadAdd(137, 42);
这里的问题是令牌粘贴形成了一个预处理令牌137+42
。预处理器然后尝试将此预处理标记映射到单个逻辑标记,但没有可以对应的单个标记。通常,C 或 C++ 会将其视为三个单独的标记(137、+ 和 42),但由于您强行将它们粘在一起,编译器不知道它在看什么。
将此与更传统的 Add 宏进行对比,后者出于说明的目的省略了大量重要的括号:
#define Add(x, y) x + y
int z = Add(137, 42);
此处,Add(137, 42) 扩展为三个标记 (137, +, 42) 的序列,随后编译器可以将其解释为加法表达式。
你上面写的宏就像BadAdd宏。通过将 -> 标记与字段名称粘合在一起,您最终会得到一个像 ->one
这样的单元,编译器无法将其有意义地解释为单个标记。只需删除此处的 ## - 就像从 BadAdd 到 Add 一样,这将生成一系列标记而不是单个标记,这正是您在这里想要的。