C ++中Trigraph序列的用途? [英] Purpose of Trigraph sequences in C++?

查看:191
本文介绍了C ++中Trigraph序列的用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C ++'03标准2.3 / 1:


在进行任何其他处理之前,

  ------ -------------------------------------------------- -------------------- 
| trigraph |替换| trigraph |替换| trigraph |替换|
---------------------------------------------- ------------------------------
| Δθ= | #| (| | | | | | <| {|
| | / | | | | | ||)| ] | > | } |
| ?' | ??! | | | ?? - | 〜|
---------------------------------------------- ------------------------------


在现实生活中,代表 printf(What ??!\\\
);
将会导致打印 What | ,因为 ??!是一个三元序列, > | 字符。



我的问题是使用三字母的目的是什么?



UPD :在回答中提到一些欧洲键盘没有所有的标点符号,所以非美国程序员必须使用


$ b $ p

UPD2 :Visual Studio 2010默认情况下已关闭三重支持。

解决方案

这个问题(关于密切相关的图)有答案。



这归结为ISO 646字符集没有所有的字符的C语法,所以有一些系统的键盘和显示器不能处理的字符(虽然我想是这些是非常罕见的)现在



一般,你不需要使用它们,但你需要知道他们正是你遇到的问题。 Trigraphs是字符具有转义序列的原因:

 '\?'

因此,可以避免您的示例问题的几种方法是: p>

  printf(What?\?!\\\
);

printf(What??!\\\
);

但你必须记住当你输入两个'?'字符一个三叉(这当然不是我在想的东西)。



在实践中,三叉和有向图是我每天都不用担心的东西。但你应该意识到他们,因为每两年,你会遇到一个与他们有关的错误(你会花一天的剩余时间来诅咒他们的存在)。如果编译器可以配置为警告(或错误)当它遇到一个三字母或有向图,这将是很好,所以我可以知道我有一些我应该知道的处理。



为了完整性,有向图的危险性要小得多,因为它们被作为令牌处理,所以字符串文字中的有向图不会被解释为有向图。



对于C / C ++程序中包含标点符号的各种乐趣的教育(包括一个三叉虫会让我把我的头发拉出来),看看 Herb Sutter的GOTW#86文章






附录:



默认情况下,GCC似乎不会处理三角形。一些其他编译器有选项关闭三叉树支持(例如IBM)。 Microsoft开始支持在VS2008中的警告(C4837),必须显式启用(使用-Wall或某物)。


According to C++'03 Standard 2.3/1:

Before any other processing takes place, each occurrence of one of the following sequences of three characters ("trigraph sequences") is replaced by the single character indicated in Table 1.

----------------------------------------------------------------------------
| trigraph | replacement | trigraph | replacement | trigraph | replacement |
----------------------------------------------------------------------------
| ??=      | #           | ??(      | [           | ??<      | {           |
| ??/      | \           | ??)      | ]           | ??>      | }           |
| ??’      | ˆ           | ??!      | |           | ??-      | ˜           |
----------------------------------------------------------------------------

In real life that means that code printf( "What??!\n" ); will result in printing What| because ??! is a trigraph sequence that is replaced with the | character.

My question is what purpose of using trigraphs? Is there any practical advantage of using trigraphs?

UPD: In answers was mentioned that some European keyboards don't have all the punctuation characters, so non-US programmers have to use trigraphs in everyday life?

UPD2: Visual Studio 2010 has trigraph support turned off by default.

解决方案

This question (about the closely related digraphs) has the answer.

It boils down to the fact that the ISO 646 character set doesn't have all the characters of the C syntax, so there are some systems with keyboards and displays that can't deal with the characters (though I imagine that these are quite rare nowadays).

In general, you don't need to use them, but you need to know about them for exactly the problem you ran into. Trigraphs are the reason the the '?' character has an escape sequence:

'\?'

So a couple ways you can avoid your example problem are:

 printf( "What?\?!\n" ); 

 printf( "What?" "?!\n" );

But you have to remember when you're typing the two '?' characters that you might be starting a trigraph (and it's certainly never something I'm thinking about).

In practice, trigraphs and digraphs are something I don't worry about at all on a day-to-day basis. But you should be aware of them because once every couple years you'll run into a bug related to them (and you'll spend the rest of the day cursing their existance). It would be nice if compilers could be configured to warn (or error) when it comes across a trigraph or digraph, so I could know I've got something I should knowingly deal with.

And just for completeness, digraphs are much less dangerous since they get processed as tokens, so a digraph inside a string literal won't get interpreted as a digraph.

For a nice education on various fun with punctuation in C/C++ programs (including a trigraph bug that would defintinely have me pulling my hair out), take a look at Herb Sutter's GOTW #86 article.


Addendum:

It looks like GCC will not process (and will warn about) trigraphs by default. Some other compilers have options to turn off trigraph support (IBM's for example). Microsoft started supporting a warning (C4837) in VS2008 that must be explicitly enabled (using -Wall or something).

这篇关于C ++中Trigraph序列的用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆