3 ms·
> Overloaded operators like operator*() and operator<<() are widely used in the standard library so you're forced to understand what craziness they're doing und
by jcelerier 1y ago
> Overloaded operators like operator*() and operator<<() are widely used in the standard library so you're forced to understand what craziness they're doing under the hood
you don't need to understand what an overloaded operator is doing any more than you have to understand the implementation of every function you call, recursively
- zbentley 1y agoI mean, you kinda do. Otherwise you won’t understand why bit-shifting to std::cout prints something, which is pretty much day 1 of C++ hello world introduction (yes, I know there are introductions that don’t use that silly syntax sugar. They’re rare, like it or not.) Like, sure, you don’t have to understand cout’s implementation of operator <<, but you have to know a) that it’s overloadable in the first place, b) that overloads can be arbitrary functions on arbitrary types (surprising if coming from languages that support more limited operator overloading), and c) probably how to google/go-to-documentation on operators for a type to see what bit-shifting a string into stdio does. That’s … a lot more to learn than, say, printf.
- jcelerier 1y ago> I mean, you kinda do. Otherwise you won’t understand why bit-shifting to std::cout prints something, but it's not bit-shifting, it's "<<", which can do different operations in different contexts.
- zbentley 1y ago…yes, that is what I said people need to understand in operator-overloading-happy environments.
- jcelerier 1y agoThe problem is having the idea than an operator can only represent one thing while in math there's no such rule - people should start programming without the broken mindset that an operator only means one thing, and then things become much easier. Operators are functions, functions can be overloaded and come with default overloads for specific types: adding a special case makes it more complex.