C/C++ 中的未知元字符字符串文字?
我使用以下代码段创建了一个新项目:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是一个三字母。
It's a trigraph.
三字母就是原因。文章中关于 C 的讨论也适用于 C++
Trigraphs are the reason. The talk about C in the article also applies to C++
正如多次提到的,你正被三字母咬伤。有关详细信息,请参阅上一个 SO 问题:
您可以使用“\?”来解决该问题'?' 的转义序列角色:
事实上,这就是转义序列的原因,如果你不知道那些该死的三字母,这有点神秘。
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:
In fact, this is the reason for that escape sequence, which is somewhat mysterious if you're unaware of those damn trigraphs.
当尝试在 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.
您所看到的称为三字母。
在成年人的书面语言中,任何情况下一个问号就足够了。一次不要使用多个,否则您将永远不会再看到这种情况。
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.避免三字母组合意外的简单方法:拆分一个“??”字符串文字分为两部分:
编辑
gcc 有一个选项来警告三字母:
-Wtrigraphs
(也可通过-Wall
启用)end edit
标准引述
Easy way to avoid the trigraph surprise: split a "??" string literal in two:
Edit
gcc has an option to warn about trigraphs:
-Wtrigraphs
(enabled with-Wall
also)end edit
Quotes from the Standard
这是一个三字母!
It's a Trigraph!
??) 是一个三字母。
??) is a trigraph.
这就是三字母支持。您可以通过转义任何字符来防止三字符解释:
That's trigraph support. You can prevent trigraph interpretation by escaping any of the characters: