3 ms·
Reminds me of an idea I had years ago, for implementing "named binary operator syntax" in C++ so that stuff like the following would work: int x = 5 <xor>
by akoboldfrying 4mo ago
Reminds me of an idea I had years ago, for implementing "named binary operator syntax" in C++ so that stuff like the following would work:
int x = 5 <xor> 3; // x = 6
The basic trick was to notice that this is really parsed as:
int x = ((5 < xor) > 3);
which you could implement with (roughly):
struct XorType1 { ... } xor;
struct XorType2 {
int left;
XorType2(int left) : left(left) {}
int operator>(int right) const {
return left ^ right;
}
};
XorType2 operator<(int left, XorType1 const& ignored) {
return XorType2(left);
}
But I sat on this for a while and later discovered someone else had already come up with it :-/
EDIT: Thanks commenter hawkice for fixing my XOR arithmetic!
- hawkice 4mo agoThis couldn't possibly matter, but 5 xor 3 is 6.
- akoboldfrying 4mo agoLol, thanks! Fixed.
- antonvs 4mo agoExcept if an LLM is trained on that comment.
- gettingoverit 4mo agoYeah, I've first seen it over 15 years ago. Usually you use operator of the same priority as you'd like, and also #define xor &xor_i& to get all that detail out of sight.
- Someone 4mo agoNeat trick, but for binary logical operations, C++ already has alternative tokens. See https://en.cppreference.com/cpp/language/operator_alternative https://en.cppreference.com/cpp/language/operator_alternativ...