代码之家  ›  专栏  ›  技术社区  ›  Marius

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

  •  6
  • Marius  · 技术社区  · 15 年前

    char* strange = "(Strange??)";
    cout << strange << endl;
    

    产生以下输出:

    (奇怪的)

    调试结果表明,我的char*string文本实际上就是这个值,而不是流翻译。这显然不是我见过的元字符序列。 可能是某种Unicode或宽字符序列?但是我不这么认为。。。我已尝试禁用所有相关的项目设置,但无效。

    有人有解释吗?

    • 搜索:“问号,问号,紧括号”C++ C++字符串
    9 回复  |  直到 15 年前
        1
  •  17
  •   Rob Kennedy    15 年前

    你所看到的被称为 trigraph

    默认情况下,GCC忽略三角图,因为几乎没有人故意使用它们。使用 -trigraph 选项,或告诉编译器使用 -Wtrigraphs

    /Zc:trigraphs

        2
  •  6
  •   pmg    15 年前

    避免trigraph意外的简单方法:将“?”字符串文本一分为二:

    char* strange = "(Strange??)";
    char* strange2 = "(Strange?" "?)";
    /*                         ^^^ no punctuation */
    

    编辑
    -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.
    
        3
  •  5
  •   Adam Wright    15 年前

    这是一个 Trigraph !

        4
  •  4
  •   Carl Norum    15 年前
        5
  •  4
  •   R Samuel Klatchko    15 年前

    那是 trigraph

    char* strange = "(Strange?\?)";
    
        6
  •  3
  •   Sydius    15 年前

    这是一个 trigraph .

        7
  •  3
  •   jmucchiello    15 年前

    Trigraphs 这就是原因。文中的C语言也适用于C++

        8
  •  2
  •   Community    7 年前

    如前几次所述,你被三角描记器咬伤了。有关更多信息,请参见上一个SO问题:

    您可以通过使用“?”字符的“\?”转义序列来解决此问题:

    char* strange = "(Strange\?\?)";
    

    事实上,这就是逃逸序列的原因,如果你不知道那些该死的三角图,这就有点神秘了。

        9
  •  1
  •   Marius    15 年前

    当试图在GCC上交叉编译时,它将我的序列作为 trigraph :

    所以我现在需要做的就是找出如何在默认情况下在项目中禁用它,因为我只能看到它给我带来了问题(我使用的是美国键盘布局)