Last updated:2026-10-12
C++ Trigraphs¶
C++那些事: Removing Trigraphs¶
Before C++17, trigraphs were a facility for writing programs with small character codes such as ISO/IEC 646, introduced in C89. ISO/IEC 646 lacks the following characters used in C, so ?? is used to escape them as follows:
| Trigraph | Replacement |
|---|---|
??= |
# |
??( |
[ |
??) |
] |
??< |
{ |
??> |
} |
??/ |
\ |
??! |
| |
??- |
~ |
??' |
^ |
\? is escaped to prevent it from being replaced by the trigraph table.
Trigraphs were removed in C++17, and ??= and the like are no longer replaced.
Example¶
Before C++17:
Output:
C++17 and later:
Nowadays, presumably because character encodings such as Unicode are ubiquitous, trigraphs are no longer needed, and compilers such as gcc disable them by default.
Related Topics¶
- C++ Avoiding the Default Constructor | C++那些事
- C++ Class Initializer List Idiom | C++那些事
- C++ Prefer Class Templates over Function Templates | C++那些事
- C++ copy-and-swap Idiom | C++那些事
- C++ Dependency Injection | C++那些事
- C++ enum class Strongly Typed Enums | C++那些事
- C++ Header-Only vs Implementation Separation | C++那些事
- C++ Lazy Evaluation Idiom | C++那些事
- C++ Lookup Table Idiom | C++那些事
- C++ Member Pointers and Type Information | C++那些事