<分区>
下面的代码应该输出 6
,但它却输出 5
。我不知道为什么。怎么回事?
#include <iostream>
template <typename T>
void foo(T& y)
{
y++;
}
int main()
{
int x = 5;
// Why won't this line work???/
foo(x);
std::cout << x;
}
<分区>
下面的代码应该输出 6
,但它却输出 5
。我不知道为什么。怎么回事?
#include <iostream>
template <typename T>
void foo(T& y)
{
y++;
}
int main()
{
int x = 5;
// Why won't this line work???/
foo(x);
std::cout << x;
}
最佳答案
您正在使用 trigraphs 的好技巧.
// Why won't this line work???/
| |
\ /
|
~trigraph~
??/
三字母依次转换为 \
,它基本上将当前行与下一行连接起来,因此您的代码或多或少会变成这样:
// Why won't this line work? foo(x);
确实是个绝招。
引用自 C++11 标准:
§2.2.2:
Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. ...
§2.4.1:
Table 1 - Trigraph sequences
...
==========================
| Trigraph | Replacement |
==========================
| ... |
==========================
| ??/ | \ |
==========================
幸运的是,GCC seems to detect this kind of trickery ,发出警告(只需设置 -Wall
):
main.cpp:13:32: warning: trigraph ??/ converted to \ [-Wtrigraphs]
// Why won't this line work???/
^
main.cpp:13:4: warning: multi-line comment [-Wcomment]
// Why won't this line work???/
^
<子> 相关引用:
What is this smiley-with-beard expression: "<:]{%>"?
What does the C ??!??! operator do?
还有所有其他类似的问题。 ??)
PS:那是笑脸。
关于c++ - 递增引用变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17442912/