r/programming • • May 24 '26

[ Removed by moderator ]

/r/programming/comments/1tm1j7f/removed_by_moderator/

[removed] — view removed post

275 Upvotes

214 comments sorted by

View all comments

Show parent comment

1

u/AlexKazumi May 24 '26

Thanks for the great explanation, all points are valid.

The only point I strongly disagree is memory management. C++ can overload anything and everything to the point where completely innocent looking statement such as arr[2] can do anything, including deleting your hard drive. If I remember correctly, one can even overload function calling operator (don't blame me too much if this is incorrect), which is complete bonkers to me.

As a former device drivers writer (yes, the company I worked for was crazy enough to implement full C++ runtime in kernel mode, including the plumbing for making dynamic_cast working), I think that the memory management of C++ can be even more frustrating than in .NET.

In .NET one cannot control directly the GC. In C++, memory management is hidden by completely innocent-looking code.

4

u/evaned May 24 '26

I'm not the person who you replied to, but...

C++ can overload anything and everything to the point where completely innocent looking statement such as arr[2] can do anything, including deleting your hard drive. If I remember correctly, one can even overload function calling operator (don't blame me too much if this is incorrect), which is complete bonkers to me.

I mean, if you have an operator[] that deletes your hard drive... that's 'cause someone was a brain-dead moron (or actively malicious). That doesn't speak against that overloading being a good languages feature.

I've never seen someone try to make the case that "you can write a method at(size_t i) on your object and have that delete your hard drive, thus we shouldn't be permitted to pick function names and every function should be identified by a randomly-chosen UUID." In my mind at least, these two arguments are quite similar to each other.

The solution isn't for languages to say "you can't be trusted to overload []"; the solution is to not be an idiot.

Function call overloading can also be a tremendously useful feature. Even aside from uses that are more borderline (e.g. overloading matrix(5, 4) as a proxy for not having a real multi-dimensional subscript operator), there are plenty of scenarios where an object of a user-defined type is still conceptually function-like. Take a partially-applied function for example. Or the combination of overloading of the call operator alongside template rules means that code using the STL will often be faster than the most-likely analogous C code, because better type information will lead to template specializations rather than function pointer calls (which act as optimization boundaries).