3 ms·
This is a misunderstanding of C++ principles, not an anti-C++ rant. In C++, it is intentionally not straightforward to know what code gets called when a statem
by einpoklum 4y ago
This is a misunderstanding of C++ principles, not an anti-C++ rant.
In C++, it is intentionally not straightforward to know what code gets called when a statement is executed - even without namespaces. We have:
* Function overloads
* Non-trivial (and not-built-in) implicit conversions
* Non-trivial copy and move constructors
* Template and concept resolution, which can be tricky
* Operator overloads, so that foo[n] or bar() may do something very different than what you expect.
And if you add in macros to the mix, you can really go crazy. For example, in this SO answer: https://stackoverflow.com/a/70328397/1593077 https://stackoverflow.com/a/70328397/1593077 I explain how to implement an infix operator which lets us write:
int x = whatever();
if ( x is_one_of {1, 3, 42, 7, 69, 550123} ) {
do_stuff(x);
}
C is simply not that kind of language (well, macros notwithstanding I suppose).
- jstimpfle 4y agoThis is nice if your goal is to come up with convoluted ways to say the same thing in fewer characters (ignoring the code that you had to add to enable the shorter version in the first place). With your example, both a switch and simple if-else chains would be perfectly readable and also easy to write.