Purpose of Trigraph sequences in C++?

2019-01-04 18:59发布

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.

9条回答
Evening l夕情丶
2楼-- · 2019-01-04 19:47

Some European keyboards don't (didn't?) have all the punctuation characters that US keyboards had, because they needed the keys for their unusual alphabetic characters. So for example (making this up), the Swedish keyboard would have A-ring where the curly brace was.

To accommodate those users, trigraphs are a way to enter punctuation using only the most common ASCII characters.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-04 19:49

Trigraphs have been proposed for removal in C++0x. That said, there still seems to be strong argument in support of them - see C++ committee paper N2910 which discusses this. Apparently, EBCDIC is one major stronghold where they are needed.

查看更多
【Aperson】
4楼-- · 2019-01-04 19:50

Primarily because the C standard introduced them back in 1989, when there were issues with the presence of the characters that trigraphs map to on some machines. By the time the C++ standard was published in 1998, the need for trigraphs was not great. They are a wart on C; they are just as much a wart on C++. There was a need for them - especially outside the English-speaking world - which is why they were added to C.

查看更多
登录 后发表回答