C++ (and C) source code may be written in any non-ASCII 7-bit character set that includes the ISO 646:1983 invariant character set. However, several C++ operators and punctuators require characters that are outside of the ISO 646 codeset: {, }, [, ], #, \, ^, |, ~
.
To be able to use character encodings where some or all of these symbols do not exist (such as the German DIN 66003), C++ defines the following alternatives composed of ISO 646 compatible characters.
There are alternative spellings for several operators and other tokens that use non-ISO646 characters. In all respects of the language, each alternative token behaves exactly the same as its primary token, except for its spelling (the stringification operator can make the spelling visible). The two-letter alternative tokens are sometimes called “digraphs”.
Primary | Alternative |
---|---|
&& | and |
&= | and_eq |
& | bitand |
| | bitor |
~ | compl |
! | not |
!= | not_eq |
|| | or |
|= | or_eq |
^ | xor |
^= | xor_eq |
{ | <% |
} | %> |
[ | <: |
] | :> |
# | %: |
## | %:%: |
Knowing this, you can create code in this style: vim main.cpp
If you compile and run, the output will be:
The same words are defined in the C programming language in the <iso646.h>
include file as macros. Because in C++ they are built into the language.
Example in C language: vim main.c
There are still the Trigraphs too, but they were taken from C++17, they are:
Primary | Trigraphs |
---|---|
{ | ??< |
} | ??> |
[ | ??( |
] | ??) |
# | ??= |
\ | ??/ |
^ | ??’ |
| | ??! |
~ | ??- |
Macros are a really scary thing, if you create a header like this: vim troll.hpp
And include it in your source: vim main.cpp
:
And compile, it will run your code:
That’s crazy, right?! You can also do it using a single letter, example: #define e int
, define ee main
, … and so on!