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

发布于 2024-08-09 15:54:20 字数 382 浏览 5 评论 0原文

我使用以下代码段创建了一个新项目:

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

产生以下输出:

(奇怪)

这样翻译'??)' -> ']'

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

有人有解释吗?

  • 搜索:'问号,问号,右大括号'c c++ 字符串文字

I created a new project with the following code segment:

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

resulting in the following output:

(Strange]

Thus translating '??)' -> ']'

Debugging it shows that my char* string literal is actually that value and it's not a stream translation. This is obviously not a meta-character sequence I've ever seen.
Some sort of Unicode or wide char sequence perhaps? I don't think so however... I've tried disabling all related project settings to no avail.

Anyone have an explanation?

  • search : 'question mark, question mark, close brace' c c++ string literal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

只有一腔孤勇 2024-08-16 15:54:21

这是一个三字母

It's a trigraph.

征棹 2024-08-16 15:54:21

三字母就是原因。文章中关于 C 的讨论也适用于 C++

Trigraphs are the reason. The talk about C in the article also applies to C++

秋意浓 2024-08-16 15:54:21

正如多次提到的,你正被三字母咬伤。有关详细信息,请参阅上一个 SO 问题:

您可以使用“\?”来解决该问题'?' 的转义序列角色:

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

事实上,这就是转义序列的原因,如果你不知道那些该死的三字母,这有点神秘。

As mentioned several times, you're being bitten by a trigraph. See this previous SO question for more information:

You can fix the problem by using the '\?' escape sequence for the '?' character:

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

In fact, this is the reason for that escape sequence, which is somewhat mysterious if you're unaware of those damn trigraphs.

[旋木] 2024-08-16 15:54:21

当尝试在 GCC 上进行交叉编译时,它选择了我的序列作为 trigraph

所以我现在需要做的就是弄清楚如何默认在项目中禁用它,因为我只能看到它产生问题为我。 (无论如何,我使用的是美国键盘布局)

GCC 上的默认行为是忽略但发出警告,这更加明智,而且据我所知,这确实是 Visual Studio 2010 将采用的标准。

While trying to cross-compile on GCC it picked my sequence up as a trigraph:

So all I need to do now is figure out how to disable this in projects by default since I can only see it creating problems for me. (I'm using a US keyboard layout anyway)

The default behavior on GCC is to ignore but give a warning, which is much more sane and is indeed what Visual Studio 2010 will adopt as the standard as far as I know.

离去的眼神 2024-08-16 15:54:20

您所看到的称为三字母

在成年人的书面语言中,任何情况下一个问号就足够了。一次不要使用多个,否则您将永远不会再看到这种情况。

GCC 默认忽略三字母组,因为几乎没有人有意使用它们。使用 -trigraph 选项启用它们,或者使用 -Wtrigraphs 选项告诉编译器警告您有关它们的信息。

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

What you're seeing is called a trigraph.

In written language by grown-ups, one question mark is sufficient for any situation. Don't use more than one at a time and you'll never see this again.

GCC ignores trigraphs by default because hardly anyone uses them intentionally. Enable them with the -trigraph option, or tell the compiler to warning you about them with the -Wtrigraphs option.

Visual C++ 2010 also disables them by default and offers /Zc:trigraphs to enable them. I can't find anything about ways to enable or disable them in prior versions.

白色秋天 2024-08-16 15:54:20

避免三字母组合意外的简单方法:拆分一个“??”字符串文字分为两部分:

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

编辑
gcc 有一个选项来警告三字母:-Wtrigraphs(也可通过 -Wall 启用)
end edit

标准引述

    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.

Easy way to avoid the trigraph surprise: split a "??" string literal in two:

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

Edit
gcc has an option to warn about trigraphs: -Wtrigraphs (enabled with -Wall also)
end edit

Quotes from the Standard

    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.
情域 2024-08-16 15:54:20

这是一个三字母

It's a Trigraph!

娇纵 2024-08-16 15:54:20

??) 是一个三字母

??) is a trigraph.

苦妄 2024-08-16 15:54:20

这就是三字母支持。您可以通过转义任何字符来防止三字符解释:

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

That's trigraph support. You can prevent trigraph interpretation by escaping any of the characters:

char* strange = "(Strange?\?)";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文