什么是宏观扩张的具体步骤? [英] What's the exact step of macro expanding?

查看:119
本文介绍了什么是宏观扩张的具体步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这没有按照预期工作:

#define stringify(x) #x  
printf("Error at line " stringify(__LINE__));

本作品:

#define stringify1(x) #x  
#define stringify(x) stringify1(x)  
printf("Error at line " stringify(__LINE__));  

那是什么preprocess优先用于扩大这些宏?

What's the priority that preprocess uses to expand such macros?

推荐答案

在扩展宏,在preprocessor宏扩展的参数的只有在这些参数不进行字符串化()或令牌粘贴( ## )操作符的。所以,如果你有这样的:

When expanding a macro, the preprocessor expands the macro's arguments only if those arguments are not subjected to the stringizing (#) or token-pasting (##) operators. So, if you have this:

#define stringify(x) #x
stringify(__LINE__)

然后,preprocessor做的的展开 __ __ LINE ,因为它是字符串化运营商的说法。但是,当你这样做:

Then, the preprocessor does not expand __LINE__, because it's the argument of the stringizing operator. However, when you do this:

#define stringify1(x) #x
#define stringify(x) stringify1(x)
stringify(__LINE__)

然后,扩大在字符串化中,preprocessor扩张 __ LINE __ 当前行号,因为 X 未与任一字符串化或令牌粘贴运营商字符串化的定义中使用。然后,它扩展了 stringify1 ,我们得到我们想要的东西。

Then, when expanding stringify, the preprocessor expands __LINE__ to the current line number, since x is not used with either the stringizing or token-pasting operators in the definition of stringify. It then expands stringify1, and we get what we wanted.

从C99标准的相关语言来自§6.10.3.1/ 1:

The relevant language from the C99 standard comes from §6.10.3.1/1:

一个函数宏的调用的参数已经确定后,参数替换发生。在替换列表中的参数,除非$ P $由 ## preprocessing令牌或pceded其次是 ## preprocessing令牌(见下文),由相应的参数后更换其中所含的所有宏已经扩大。之前被取代,每个参数的preprocessing令牌是完全宏,如果他们形成了preprocessing文件的其余部分更换;没有其他的preprocessing令牌是可用的。

After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are completely macro replaced as if they formed the rest of the preprocessing file; no other preprocessing tokens are available.

第§6.10.3.2和6.10.3.3去定义的行为 ## 运营商分别。

Clauses §6.10.3.2 and 6.10.3.3 go on to define the behavior of the # and ## operators respectively.

这篇关于什么是宏观扩张的具体步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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