Home
Cursed C++: Alternative Operators
C++ alternative operators is a legacy feature that truly makes any codebase cursed.
%:include <iostream>
struct A <%
A(int bitand a) : a(a) {}
int bitand a;
%>;
int main(int argc, char**argv)
<%
if(argc not_eq 2) <% return 1;%>
int n = std::atoi(argv<:1:>);
A a(n);
auto func = <:bitand:>(A a)<%
std::cout << a.a << std::endl;
%>;
func(a);
return 0;
%>
You might not believe it, but this code compiles and prints the integer provided as an argument.
List of alternative operators
This is the list of alternative operators in C++.
Primary | Alternative |
&& | and |
&= | and_eq |
& | bitand |
| | bitor |
~ | compl |
! | not |
!= | not_eq |
|| | or |
|= | or_eq |
^ | xor |
^= | xor_eq |
{ | <% |
} | %> |
[ | <: |
] | :> |
# | %: |
## | %:%: |
The most notable one is bitand which can hilariously be used to indicate a reference as shown in the example above.
What are these for?
C source code must be compatible with the ISO 646 invariant character set. This non-ascii character set was developed as a base for international 7-bit variant character sets. Due to C++ backwards compatibility with C, these alternative operators are also in C++ today.
ISO 646 invariant structure
The invariant character set has 34 control codes (characters such as NUL and DEL) and 82 non-control characters.
Here are the 82 non-control characters:
!"%&'()*+,-./0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz
You might notice that these characters {}[] are missing. This is why alternative operators were added in C.
The invariant leaves 12 characters for international variants.
As an example of international use, this is how the french-canadian variant used its 12 spare characters: #$àâçêîôéùèû.
It is a bit mysterious why & needs an alternate operator when it is in the invariant. It appears that some international variants went beyond the 12 characters and replaced other characters like !. I could not find a specific variant replacing & but the ! character is supported with the not keyword in C++, leading me to believe that & is supported because there's a variant not using it.
Questions? Comments? Insults? Send me an email!
moc.snetramcirdec@cirdec
Created: 2022-11-26 - Last Edited: 2022-11-26