4 ms·
I really like The Hacks of Life and think it's one of the best blogs on the Internet, but this time they really got it wrong. The title should not be The Dange
by rsp1984 11y ago
I really like The Hacks of Life and think it's one of the best blogs on the Internet, but this time they really got it wrong.
The title should not be The Dangers of Super Smart Compilers but rather The Dangers of Engineers Trying to be Super Smart with C++. Yes that's right. Having operator== and operator!= overloads for stuff like pointers in your code means that you're either a 0.01% Super Engineer who 100% of the time knows what the f* he's doing and knows that it's actually necessary for the problem at hand (overall not so likely), or it means that your code is unnecessarily complicated and there's an easier and more elegant and more readable solution if you take a step back and re-factor some stuff (more likely, and yes sometimes it means taking a lot of steps back).
But please when stuff goes wrong, don't blame the compiler first.
- crazy2be 11y agoWhat? These are overloads for smart pointers, not regular pointers, and thus require special code to get the semantics of regular pointers that are usually expected and desired by developers. Even the standard library unique_ptr overloads these operators (http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_cmp http://en.cppreference.com/w/cpp/memory/unique_ptr/operator_...). Of course, you could argue that smart pointers shouldn't be a thing, but I think most C++ developers would disagree given the significant advantage of essentially automatic ownership management, at least for tree-like structures (cycles are more complicated, as always). What would you propose as a simpler solution? Many languages have GC, which is arguably the simpler solution you speak of, but would be unacceptable in C++, since the large majority of applications it is used for require predictable low-latency responses. Manually managing pointers is how you do things in C, but has it's own sets of problems, since it can't be checked in any way by the compiler, so things like forgetting to free, double free, use after free, etc, are common, while with smart pointers these errors are impossible.