应该输出以下代码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;
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*cia 21
你正在使用三元组的好玩法.
// Why won't this line work???/
| |
\ /
|
~trigraph~
Run Code Online (Sandbox Code Playgroud)
然后将??/三字形转换为\基本上将当前行与下一行连接起来,因此您的代码或多或少会像这样:
// Why won't this line work? foo(x);
Run Code Online (Sandbox Code Playgroud)
确实很好的伎俩.
引用C++ 11标准:
§2.2.2:
删除反斜杠字符(\)后面紧跟一个新行字符的每个实例,拼接物理源代码行以形成逻辑源代码行....
第2.4.1节:
Table 1 - Trigraph sequences
...
==========================
| Trigraph | Replacement |
==========================
| ... |
==========================
| ??/ | \ |
==========================
Run Code Online (Sandbox Code Playgroud)
幸运的是,GCC似乎发现了这种诡计,发出警告(只是设置-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???/
^
Run Code Online (Sandbox Code Playgroud)
相关参考:
还有其他所有类似的问题.??)
PS:那是个笑脸.
??/是一个被替换为的Trigraph序列\.
对于编译器\意味着后面的立即行是当前行的一部分.在这种情况下,当前行是注释.有效的结果是:
// Why won't this line work foo(x);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
722 次 |
| 最近记录: |