宏连接中有效的预处理器令牌 [英] Valid preprocessor tokens in macro concatenation

查看:99
本文介绍了宏连接中有效的预处理器令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用串联预处理器运算符##来理解c中的宏,但是我意识到我有令牌问题。我认为这很容易,但实际上并非如此。

所以连接是为了连接两个令牌来创建一个新的令牌。
ex:连接 int *



我试过

  #define foo(x,y)x ## y 
foo(x,y)

每当我给它一些参数时总会有错误说<粘贴两个参数都不会给出有效的预处理器标记。



例如,为什么连接 foo(1,aa)导致 1aa 强>哪种类型的令牌是它?以及为什么它是有效的)
$ b

有没有办法知道哪些标记是有效的或者是否有可能建立一些良好的链接来理解如何在我的脑海中澄清它。 (我已经在Google和Google搜索)



我缺少什么?



我将不胜感激。 / p>

解决方案

预处理器令牌连接用于生成新的令牌,但它不能将任意语言构造粘贴在一起(例如, gcc文档):


但是,两个不共同形成有效令牌的令牌不能被
粘贴在一起。例如,您不能将x与
中的+连接在一起。


所以试图创建一个指针(例如

  #define MAKEPTR(NAME)NAME ## * 
MAKEPTR(int)myIntPtr;

是无效的,因为 int * 是两个令牌,而不是一个。



然而,上述链接的示例显示了新代币的生成:

 #define COMMAND(NAME){#NAME,NAME ## _command} 

struct命令命令[] =
{
COMMAND退出),
COMMAND(帮助),
...
};

产生:

  struct command commands [] = 
{
{quit,quit_command},
{help,help_command},
...
};

令牌 quit_command 通过令牌串联生成。



请注意,形式的宏

  #define MAKEPTR(TYPE)TYPE * 
MAKEPTR(int)myIntPtr;

是有效的,并且实际上会生成一个指针类型,其中 TYPE ,例如 int * out of int


I tried to understand the macros in c using the concatenation preprocessor operator ## but I realized that I have problem with tokens. I thought it was easy but in practice it is not.

So the concatenation is for concatenating two tokens to create a new token. ex: concatenating ( and ) or int and *

I tried

#define foo(x,y) x ## y
foo(x,y)

whenever I give it some arguments I get always error saying that pasting both argument does not give a valid preprocessor token.

For instance why concatenating foo(1,aa) results in 1aa (which type of token is it ? and why it is valid) but foo(int,*) I got an error.

Is there a way to know which tokens are valid or is it possible to have some good link to understand how can clarify it in my mind. (I already googled in google and SO)

What am I missing ?

I will be grateful.

解决方案

Preprocessor token concatenation is for generating new tokens, but it is not capable of pasting arbitrary language constructs together (confer, for example, gcc documentation):

However, two tokens that don't together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order.

So an attempt at a macro that makes a pointer out of a type like

#define MAKEPTR(NAME)  NAME ## *
MAKEPTR(int) myIntPtr;

is invalid, as int* are two tokens, not one.

The example of above mentioned link, however, shows the generation of new tokens:

 #define COMMAND(NAME)  { #NAME, NAME ## _command }

 struct command commands[] =
 {
   COMMAND (quit),
   COMMAND (help),
   ...
 };

yields:

 struct command commands[] =
 {
   { "quit", quit_command },
   { "help", help_command },
   ...
 };

Token quit_command has not existed before but has been generated through token concatenation.

Note that a macro of the form

#define MAKEPTR(TYPE)  TYPE*
MAKEPTR(int) myIntPtr;

is valid and actually generates a pointer type out of TYPE, e.g. int* out of int.

这篇关于宏连接中有效的预处理器令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆