Purpose of Trigraph sequences in C++?

只谈情不闲聊 提交于 2019-12-17 04:14:44

问题


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.


回答1:


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).




回答2:


From The C++ Programming Language Special Edition, page 829

The ASCII special characters [, ], {, }, |, and \ occupy character set positions designated as alphabetic by ISO. In most European national ISO-646 character sets, these positions are occupied by letters not found in the English alphabet.

A set of trigraphs is provided to allow national characters to be expressed in a portable way using a truly standard minimal character set. This can be useful for interchange of programs, but it doesn't make it easier for people to read programs. Naturally, the long-term solution to this problem is for C++ programmers to get equipment that supports both their native language and C++ well. Unfortunately, this appears to be infeasible for some, and the introduction of new equipment can be a frustratingly slow process.




回答3:


Kids today! :-)

Yes, foreign equipment, such as an IBM 3270 terminal. The 3270 has, if I remember, no curly braces! If you wanted to write C on an IBM mini / mainframe, you had to use the wretched trigraphs for every block boundary. Fortunately, I only had to write software in C to emulate some IBM minicomputer facilities, not actually write C software on the System/36.

Look next to the "P" key:

Hmmm. Hard to tell. There is an extra button next to "carriage return", and I might have it backwards: maybe it was the "[" / "]" pair that was missing. At any rate, this keyboard would cause you grief if you had to write C.

Also, these terminals display EBCDIC, IBM's "native" mainframe character set, not ASCII (thanks, Pavel Minaev, for the reminder).

On the other hand, like the GNU C guide says: "You don't need this brain damage." The gcc compiler leaves this "feature" disabled by default.




回答4:


They are for use on systems that lack some of the characters in C++'s basic character set. Needless to say, such systems are exceedingly rare.




回答5:


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.




回答6:


I've seen trigraphs used in the early '90s to help convert PL/1 programs from a mainframe to be run/compiled/debugged on a PC.

They were dabbling with editing PL/I on the PC using a PL/I to C compiler and they wanted the code to work when moved back to the mainframe which did not support curly braces. I suggested that they could use macros like

#def BEGIN {    
#def END }  

or as a friendlier PL/I alternative

#def BEGIN ??<
#def END ??>

and if they really wanted to get fancy they could try

#ifdef MAINFRAME
    #def BEGIN ??<
    #def END ??>
#else
    #def BEGIN {    
    #def END }  
#endif

and then the program would look like it was written in Pascal. They just looked at me funny and wouldn't speak to me for the rest of the day. I don't think I blame them. :)

What killed the effort what not the tri-graphs, it was the IO system differences between the platforms. Opening files on the PC was so much different than the mainframe it would have introduced way too many kludges to keep the same code running on both.




回答7:


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.




回答8:


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.




回答9:


They are there mostly for historical reasons. Nowadays, most modern keyboards for most languages allow access to all those characters, but this used to be a problem once with some European keyboards. This is why trigraphs were invented.

If you don't know what they're for, you shouldn't use them.

It's still good to be aware of them, though, since you might accidentally and unintentionally use one in your code.



来源:https://stackoverflow.com/questions/1234582/purpose-of-trigraph-sequences-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!