c++ - 为什么在 Gnu gcc/g++ 中为三字母序列解析字符串文字?

标签 c++ gcc trigraphs

考虑这个无害的 C++ 程序:

#include <iostream>
int main() {
  std::cout << "(Is this a trigraph??)" << std::endl;
  return 0;
}

当我使用 g++ 5.4.0 版编译它时,我得到以下诊断信息:

me@my-laptop:~/code/C++$ g++ -c test_trigraph.cpp
test_trigraph.cpp:4:36: warning: trigraph ??) ignored, use -trigraphs to enable [-Wtrigraphs]
   std::cout << "(Is this a trigraph??)" << std::endl;
                                     ^

程序运行,输出符合预期:

(Is this a trigraph??)

为什么要针对三字母解析字符串文字?

其他编译器也这样做吗?

最佳答案

三字母在翻译阶段 1 中处理(但在 C++17 中被删除)。字符串文字相关处理发生在后续阶段。正如 C++14 标准指定的那样 (n4140) [lex.phases]/1.1 :

The precedence among the syntax rules of translation is specified by the following phases.

  1. Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) if necessary. The set of physical source file characters accepted is implementation-defined. Trigraph sequences ([lex.trigraph]) are replaced by corresponding single-character internal representations. Any source file character not in the basic source character set ([lex.charset]) is replaced by the universal-character-name that designates that character. (An implementation may use any internal encoding, so long as an actual extended character encountered in the source file, and the same extended character expressed in the source file as a universal-character-name (i.e., using the \uXXXX notation), are handled equivalently except where this replacement is reverted in a raw string literal.)

这是最先发生的,因为正如您在评论中所说,三字母代表的字符也需要可打印。

关于c++ - 为什么在 Gnu gcc/g++ 中为三字母序列解析字符串文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47904259/

相关文章:

c - 打印 ??和 !!不同的顺序会显示不同的输出

c++ - make/gmake 的条件依赖

c++ - OpenCV中的熵过滤器类似于matlab中的entropyfilt()函数

c++ - 未记录的 GCC 扩展 : VLA in struct

c - 局部同义变量到非精确类型

c - 我如何在 Bloodshed/Dev C++ 编译器中使用二合字母和三合字母

C++ 任何具有特定值类型的迭代器?

c++ - 坐标不对,添加不正确

gcc - 分支到 Microblaze CPU 上的中断处理程序,汇编语言

关于 ansi C 的奇怪的三字母序列