gpt4 book ai didi

c++ - C++ 预处理器中的 R 和 L 有什么特别之处?

转载 作者:IT老高 更新时间:2023-10-28 13:58:35 27 4
gpt4 key购买 nike

我通过 Visual Studio 2013 预处理器运行了以下代码。输出让我吃惊。

hello.cpp的内容:

#define A(j) #j

A(A?)
A(B?)
A(C?)
A(D?)
A(E?)
A(F?)
A(G?)
A(H?)
A(I?)
A(J?)
A(K?)
A(L?)
A(M?)
A(N?)
A(O?)
A(P?)
A(Q?)
A(R?)
A(S?)
A(T?)
A(U?)
A(V?)
A(W?)
A(X?)
A(Y?)
A(Z?)

命令:

cl /P hello.cpp

hello.i 包含:

#line 1 "hello.cpp"



"A?"
"B?"
"C?"
"D?"
"E?"
"F?"
"G?"
"H?"
"I?"
"J?"
"K?"
"L"
"M?"
"N?"
"O?"
"P?"
"Q?"
"R"
"S?"
"T?"
"U?"
"V?"
"W?"
"X?"
"Y?"
"Z?"

我在尝试调用 A(L?p:q) 时遇到了这个问题,导致“Lp:q”对我不利。

这是正确的、定义明确的 C++ 吗? C++ 中的 L 和 R 有什么特别之处?如果文件具有 .c 扩展名,则 L 和 R 被视为与字母表的其余部分相同。这与 C++11 有关吗?它一定是一个新特性,因为旧版本的 MSVS 不会以特殊方式处理 L 和 R。

我能做些什么来阻止 MSVS 2013 以这种特殊方式处理 L 和 R?

最佳答案

更新

错误报告似乎被标记为 this one 的拷贝其中有一个更新说:

A fix for this issue has been checked into the compiler sources. The fix should show up in the next major release of Visual C++.

原创

正如 remyabel 指出的,这是一个 reported bug . gccclang 都不会产生这个结果,stringizing operator #根据 Visual Studios 文档,这些是以下替代品(强调我的 future ):

White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.

Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark (") or backslash () character), the necessary escape backslash is automatically inserted before the character.

对应于C++ draft standard 16.3.2 部分 # 运算符 说:

If, in the replacement list, a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument. Each occurrence of white space between the argument’s preprocessing tokens becomes a single space character in the character string literal. White space before the first preprocessing token and after the last preprocessing token comprising the argument is deleted. Otherwise, the original spelling of each preprocessing token in the argument is retained in the character string literal, except for special handling for producing the spelling of string literals and character literals: a \ character is inserted before each " and \ character of a character literal or string literal (including the delimiting " characters).

RLC++11 的唯一关系是它们与 string literals 有特殊含义。但我不明白这应该如何影响这种情况。

看起来 L\R\ 也会产生同样的问题。

他们确实记录了一个 non-compliant issue它说:

Visual C++ does not behave correctly when the # (stringize) operator is used with strings that include escape sequences. In this situation, the compiler will generate Compiler Error C2017.

不包括这种情况。

关于c++ - C++ 预处理器中的 R 和 L 有什么特别之处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21098486/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com