C/C++字符串文字中的未知元字符?

Mar*_*ius 6 c c++ trigraphs visual-c++

我创建了一个包含以下代码段的新项目:

char* strange = "(Strange??)";
cout << strange << endl;
Run Code Online (Sandbox Code Playgroud)

产生以下输出:

(奇怪]

因此翻译'??)' - >']'

调试它显示我的char*字符串文字实际上是该值,它不是流转换.这显然不是我见过的元字符序列.某种Unicode或宽字符序列也许?我不这么认为......我试过禁用所有相关的项目设置无济于事.

有人有解释吗?

  • 搜索:'问号,问号,闭括号'c c ++ string literal

Rob*_*edy 17

你所看到的被称为三卦.

在成年人的书面语言中,一个问号就足以适用于任何情况.不要一次使用多个,你再也不会看到这个.

GCC默认忽略了三字母,因为几乎没有人故意使用它们.使用该-trigraph选项启用它们,或者告诉编译器使用该-Wtrigraphs选项向您发出警告.

Visual C++ 2010默认情况下也禁用它们并提供/Zc:trigraphs启用它们.我在以前的版本中找不到任何关于启用或禁用它们的方法.

  • 侮辱性暗示Marius不是一个真正有必要的成年人吗?我一直在坚持这个假设它是以幽默的方式投票,但它真的相当贬低而且完全没必要. (9认同)
  • FWIW,你可以生成“??” 很容易在代码中作为“成长”。在 DOS/Win32 中,“?” 是文件系统名称的通配符,因此“log??.txt”将代表“log00.txt”、“logAB.txt”等... (2认同)

pmg*_*pmg 6

避免三角形惊喜的简单方法:拆分"??" 字符串文字分为两部分:

char* strange = "(Strange??)";
char* strange2 = "(Strange?" "?)";
/*                         ^^^ no punctuation */
Run Code Online (Sandbox Code Playgroud)

编辑
gcc有一个选项来警告三字符:( 也-Wtrigraphs启用-Wall)
结束编辑

来自标准的报价

    5.2.1.1 Trigraph sequences
1   Before any other processing takes place, each occurrence of one of the
    following sequences of three characters (called trigraph sequences13))
    is replaced with the corresponding single character.
           ??=      #               ??)      ]               ??!      |
           ??(      [               ??'      ^               ??>      }
           ??/      \               ??<      {               ??-      ~
    No other trigraph sequences exist. Each ? that does not begin one of
    the trigraphs listed above is not changed.
    5.1.1.2 Translation phases
1   The precedence among the syntax rules of translation is specified by
    the following phases.
         1.   Physical source file multibyte characters are mapped, in an
              implementation-defined manner, to the source character set
              (introducing new-line characters for end-of-line indicators)
              if necessary. Trigraph sequences are replaced by corresponding
              single-character internal representations.


Ada*_*ght 5

这是一个Trigraph!