[英]Why do parens prevent macro substitution?
While researching solutions to the windows min
/ max
macro problem, I found an answer that I really like but I do not understand why it works. 在研究windows
min
/ max
宏问题的解决方案时,我找到了一个我非常喜欢的答案,但我不明白它为什么会起作用。 Is there something within the C++ specification that says that macro substitution doesn't occur within parens? C ++规范中是否有一些内容表明在parens中不会发生宏替换? If so where is that?
如果是这样的话呢? Is this just a side effect of something else or is the language designed to work that way?
这只是其他东西的副作用,还是那种以这种方式工作的语言? If I use extra parens the
max
macro doesn't cause a problem: 如果我使用额外的parens
max
不会导致问题:
(std::numeric_limits<int>::max)()
I'm working in a large scale MFC project, and there are some windows libraries that use those macros so I'd prefer not to use the #undef
trick. 我正在大规模的MFC项目中工作,并且有一些Windows库使用这些宏,所以我不想使用
#undef
技巧。
My other question is this. 我的另一个问题是这个。 Does
#undef max
within a .cpp file only affect the file that it is used within, or would it undefine max
for other compilation units? .cpp文件中的
#undef max
是否仅影响在其中使用的文件,或者是否会取消其他编译单元的max
?
Function-like macros only expand when the next thing after is an opening parenthesis. 类似函数的宏只有在下一个后面的东西是左括号时才会展开。 When surrounding the name with parentheses, the next thing after the name is a closing parenthesis, so no expansion occurs.
当用括号包围名称时,名称后面的下一个是结束括号,因此不会发生扩展。
From C++11 § 16.3 [cpp.replace]/10: 从C ++11§16.3[cpp.replace] / 10:
Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro).
类似函数的宏名称的每个后续实例后跟一个(作为下一个预处理标记引入预定处理标记序列,该标记由定义中的替换列表(宏的调用)替换)。
To answer the other question, preprocessing happens before normal compilation and linking, so doing an #undef
in an implementation file will only affect that file. 要回答另一个问题,预处理在正常编译和链接之前进行,因此在实现文件中执行
#undef
只会影响该文件。 In a header, it affects every file that includes that header. 在标头中,它会影响包含该标头的每个文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.