Question

Given that there were once reasons to use digraphs and trigraphs in C and C++, does anyone put them in code being written today? Is there any substantial amount of legacy code still under maintenance that contains them?

(Note: Here, "digraph" does not mean "directed graph." Both digraph and trigraph have multiple meanings, but the intended use here are sequences like ??= or <: to stand in for characters like # and [)

Was it helpful?

Solution

There is a proposal pending for C++1z (the next standard after C++1y will be standardized into -hopefully- C++14) that aims to remove trigraphs from the Standard. They did a case study on an otherwise undisclosed large codebase:

Case study

The uses of trigraph-like constructs in one large codebase were examined. We discovered:

923 instances of an escaped ? in a string literal to avoid trigraph replacement: string pattern() const { return "foo-????\?-of-?????"; }

4 instances of trigraphs being used deliberately in test code: two in the test suite for a compiler, the other two in a test suite for boost's preprocessor library.

0 instances of trigraphs being deliberately used in production code. Trigraphs continue to pose a burden on users of C++.

The proposal notes (bold emphasis from the original proposal):

If trigraphs are removed from the language entirely, an implementation that wishes to support them can continue to do so: its implementation-defined mapping from physical source file characters to the basic source character set can include trigraph translation (and can even avoid doing so within raw string literals). We do not need trigraphs in the standard for backwards compatibility.

OTHER TIPS

I don't know for sure, but you're most likely to find digraphs and trigraphs being used in IBM mainframe environments. The EBCDIC character set doesn't include some characters that are required for C.

The other justification for digraphs and trigraphs, 7-bit ASCII-ish character sets that replace some punctuation characters with accented letters, is probably less relevant today.

Outside such environments, I suspect that trigraphs are more commonly used by mistake than deliberately, as in:

puts("What happened??!");

For reference, trigraphs were introduced in the 1989 ANSI C standard (which essentially became the 1990 ISO C standard). They are:

??= #     ??) ]     ??! |
??( [     ??' ^     ??> }
??/ \     ??< {     ??- ~

The replacements occur anywhere in source code, including comments and string literals.

Digraphs are alternate spellings of certain tokens, and do not affect comments or literals:

<: [      :>   ]
<% {      %>   }
%: #      %:%: ##

Digraphs were introduced by the 1995 amendment to the 1990 ISO C standard.

The use of tri and di-graphs isn't written in this day, it exists only in very old code that was created in a very limited environment. Any code that contains trigraphs, if you attempt to compile them on a modern compiler like VS's,it will usually not compile unless you specify a linker option. I know for Visual Studio, that option is "/Zc:trigraphs"

Why they exist, is because the C++ committee never issues changes that would 'break' legacy code. For better or for worse. There is an anecdote that their removal was proposed and supported, and it was stopped by a lone IBM representative.

I know this is an old question, but there is arguably a legitimate use these days: touch screens without an actual keyboard. For example, the typical US keyboard layout isn't necessarily available in full form if you do any coding via tablet or something like that, which admittedly is hopefully rare due to how cumbersome it can be (three clicks on mine for an assignment operator). I personally don't use them if possible, but they are useful in absence of the actual tokens they're meant to represent.

Again, I really hope people avoid this where possible, but it is one reason to know and use them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top