7 ms·
C++ Alternative Operator Representations
- signa11 6y agoto see what is possible, take a look at this: https://coliru.stacked-crooked.com/view?id=4f9c171eb25285dc https://coliru.stacked-crooked.com/view?id=4f9c171eb25285dc
- tasty_freeze 6y agoThat is exactly the example in the linked article.
- ncmncm 6y agoEssential! "If new true friend not protected for explicit private union, break case and try using this." I often write if (not ... for emphasis. The "bitand" etc. are similarly useful where the operator might otherwise be mistaken for "&&" or, particularly, "||". The only one that is an unfortunate choice is "compl", which just looks strange. When I feel a need to call attention to use of "~", I define a lambda "bitwise_not(bool)". But rarely.
- thestoicattack 6y agoYou could also use std::bit_not https://en.cppreference.com/w/cpp/utility/functional/bit_not https://en.cppreference.com/w/cpp/utility/functional/bit_not
- Koshkin 6y agoYou could, but the raison d'être of these operators-as-types is their usefulness in metaprogramming.
- WalterBright 6y agoI prefer: if not nowhow not noway ...
- deleted 6y ago[deleted]
- wmu 6y agoI often use "not", "and", "or" to clearly express logical conditions. Bit operations look better as cryptic symbols "&", "|" and "~". :)
- usefulcat 6y agoI do this too, as I find it a bit more readable. Also, I find it handy to reserve '&&' exclusively for rvalue references.
- bombela 6y agoSame. `a and b or c` reads nicer than `a && b || c` to me. And I find its harder to typo and misread it: `a && b || c` vs `a & b | c` `a and b or c` vs `a & b | c`
- MaxBarraclough 6y agoA valid opinion for language design, but when writing real C++ code, I'd stick to the usual way of doing things. English language keywords aren't always great for readability in my opinion. Ada's and then / or else syntax, for instance. They're the short-circuit syntax, whereas and and or give eager evaluation. [0] You can't determine that just from the syntax though, so it ends up being no less cryptic than using strange symbols the way C++ (typically) does. [0] https://en.wikibooks.org/wiki/Ada_Programming/Operators#Short-circuit_control_forms https://en.wikibooks.org/wiki/Ada_Programming/Operators#Shor...
- TobyBrull 6y agoPlease don't!
- tirrex 6y agoWhat is the purpose of this? Does anybody find these more readable?
- detaro 6y ago> What is the purpose of this? Its explained in the first two sentences of the page.
- WalterBright 6y agoIt's like Van Halen and the brown M&Ms thing. It determines if the author of a C++ lexer, syntax highlighter, etc., read the Standard. Try splitting a digraph with \ and a newline.
- slaymaker1907 6y agoI like the use of "not" because it stands out more clearly than "!". "if (not (x and y))" seems more readable to me than "if (! (x && y))".
- tirrex 6y agoI agree, “!” is something easy to miss. I prefer “!=“ mostly for this reason. I am just surprised I’ve never seen this in any code base before.
- tyingq 6y agoIn Perl, the english operators for and/or/etc have lower precedence than the symbolic ones.
- joisig 6y agoBack in 2009 or so, I and a few others ran a Google-internal April Fool's prank based on this. We auto-generated a humongous pull request for the Chromium source code and sent it out for review by all the various source code "owners" (super reviewers) who would need to sign off. The change used the alternate encoding for curly and square brackets, claiming the original characters were inaccessible on an Icelandic keyboard layout (which is half true, and there were quite a few of us Icelanders around) and that therefore, to be inclusive, we would like to standardize on the alternative representation. Good fun, although I don't remember if a single one of the reviewers was fooled :)
- nikhilsimha 6y agoThis blew my mind. Mostly because I haven't come across this during 8 years of c++ use for professional/competitive programming.
- WalterBright 6y agoI've never seen anyone use these since they appeared in the spec decades ago. They're as pointless as trigraphs, which I've never seen in the wild either.
- jeffbee 6y agoEver seen a real use of `operator"" _X`, the C++11 feature that lets you declare a literal constant like this? int operator"" _km(unsigned long long); auto distance = 42_km; The breadth of crazy features in C++ is really huge, when you dig in.
- WalterBright 6y agoThe equivalent thing in D is, but without a special feature: template _km(ulong v) { enum _km = inKilometers(v); } auto distance = _km!42; It's used by the Sargon library to create a halffloat type: https://digitalmars.com/sargon/halffloat.html https://digitalmars.com/sargon/halffloat.html I haven't seen it used in C++, though my C++ experience post this feature is limited.
- pjmlp 6y agoAll the time, "A safer string"s Declares a std::string constant. While I agree C++ is huge, I doubt anyone would survive a Python, Java, C#, VB.NET, JavaScript, Typescript, Fortran, Ada, Common Lisp, COBOL, C pub quiz, based on the very latest language versions.
- tom_ 6y agohttps://github.com/nlohmann/json/blob/c215b77936591d4fe0aa0a1df737eb944b4ee38b/src/json.hpp#L1122 https://github.com/nlohmann/json/blob/c215b77936591d4fe0aa0a...