C++17 why not remove digraphs along with trigraphs?

3.9k views Asked by At

C++17 removed trigraphs. IBM heavily opposed this (here and here) so there seem to be arguments for both sides of removal/non removal.

But since the decision was made to remove trigraphs, why leave digraphs? I don't see any reasons for keeping digraphs beyond the reasons to keep trigraphs (which apparently didn't weight enough to keep them).

1

There are 1 answers

0
OMGtechy On BEST ANSWER

Trigraphs are more problematic to the unaware user than digraphs. This is because they are replaced within string literals and comments. Here are some examples…

Example A:

std::string example = "What??!??!";
std::cout << example << std::endl;

What|| will be printed to the console. This is because of the trigraph ??! being translated to |.

Example B:

// Error ?!?!?!??!??/
std::cout << "There was an error!" << std::endl;

Nothing will happen at all. This is because ??/ translates to \, which escapes the newline character and results in the next line being commented out.

Example C:

// This makes no sense ?!?!!?!??!??/
std::string example = "Hello World";
std::cout << example << std::endl;

This will give an error along the lines of use of undeclared identifier "example" for the same reasons as Example B.

There are far more elaborate problems trigraphs can cause too, but you get the idea. It's worth noting that many compilers actually emit a warning when such translations are being made; yet another reason to always treat warnings as errors. However this is not required by the standard and therefore cannot be relied upon.

Digraphs are much less problematic than trigraphs, as they are not replaced inside another token (i.e. a string or character literal) and there is not a sequence that translates to \, so escaping new lines in comments cannot occur.

Conclusion

Other than harder to read code, there are less problems caused by digraphs and therefore the need to remove them is greatly reduced.