←back to thread

In Defense of C++

(dayvster.com)
185 points todsacerdoti | 1 comments | | HN request time: 0.254s | source
Show context
loeg ◴[] No.45268662[source]
> in C++, you can write perfectly fine code without ever needing to worry about the more complex features of the language. You can write simple, readable, and maintainable code in C++ without ever needing to use templates, operator overloading, or any of the other more advanced features of the language.

This... doesn't really hold water. You have to learn about what the insane move semantics are (and the syntax for move ctors/operators) to do fairly basic things with the language. 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. Basic standard library datatypes like std::vector use templates, so you're debugging template instantiation issues whether you write your own templated code or not.

replies(7): >>45268759 #>>45268766 #>>45269024 #>>45272274 #>>45272736 #>>45274243 #>>45274785 #
jcelerier ◴[] No.45272736[source]
> 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

replies(1): >>45274892 #
zbentley ◴[] No.45274892[source]
I 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.

replies(1): >>45280844 #
jcelerier ◴[] No.45280844[source]
> 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.

replies(1): >>45285318 #
zbentley ◴[] No.45285318[source]
…yes, that is what I said people need to understand in operator-overloading-happy environments.
replies(1): >>45310261 #
1. jcelerier ◴[] No.45310261[source]
The 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.