Most active commenters
  • butterisgood(3)

←back to thread

In Defense of C++

(dayvster.com)
185 points todsacerdoti | 29 comments | | HN request time: 1.074s | source | bottom
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 #
butterisgood ◴[] No.45268759[source]
Overloaded operators were a terrible mistake in every programming language I've encountered them in. (Yes, sorry Haskell, you too!)

I don't think move semantics are really that bad personally, and some languages move by default (isn't that Rust's whole thing?).

What I don't like is the implicit ambiguous nature of "What does this line of code mean out of context" in C++. Good luck!

I have hope for C++front/Cpp2. https://github.com/hsutter/cppfront

(oh and I think you can write a whole book on the different ways to initialize variables in C++).

The result is you might be able to use C++ to write something new, and stick to a style that's readable... to you! But it might not make everyone else who "knows C++" instantly able to work on your code.

replies(5): >>45268857 #>>45268992 #>>45269102 #>>45271097 #>>45275305 #
1. wvenable ◴[] No.45269102[source]
Overloaded operators are great. But overloaded operators that do something entirely different than their intended purpose is bad. So a + operator that does an add in your custom numeric data type is good. But using << for output is bad.
replies(6): >>45270528 #>>45270621 #>>45272783 #>>45274942 #>>45279942 #>>45297713 #
2. ecshafer ◴[] No.45270528[source]
The first programming language that used overloaded operators I really got into was Scala, and I still love it. I love that instead of Java's x.add(y); I can overload + so that it calls .add when between two objects of type a. It of course has to be used responsibly, but it makes a lot of code really more readable.
replies(2): >>45273836 #>>45278090 #
3. LexiMax ◴[] No.45270621[source]
I will die on the hill that string concatenation should have its own operator, and overloading + for the operation is a mistake.

Languages that get it right: SQL, Lua, ML, Perl, PHP, Visual Basic.

replies(8): >>45270894 #>>45271124 #>>45272157 #>>45272324 #>>45272547 #>>45273713 #>>45273792 #>>45273877 #
4. zevets ◴[] No.45270894[source]
Alternatively, any implementation of operator+ should have a notional identity element, an inverse element and be commutative.
replies(1): >>45272328 #
5. o11c ◴[] No.45271124[source]
I think it's fine when the language has sufficiently strict types for string concatenation.

Unfortunately, many languages allow `string + int`, which is quite problematic. Java is to blame for some of this.

And C++ is even worse since literals are `const char[]` which decays to pointer.

Languages okay by my standard but not yours include: Python, Ruby.

6. paulddraper ◴[] No.45272157[source]
This is so sad obvious it’s painful.

Arithmetic addition and sequence concatenation are very very different.

——

Scala got this right as well (except strings, Java holdover)

Concatenation is ++

replies(1): >>45272561 #
7. 112233 ◴[] No.45272324[source]
Tangential, but Lua is the most write-only language I have had pleasure working with. The implementation and language design are 12 out of 10, top class. But once you need to read someone else's code, and they use overloads liberally to implement MCP and OODB and stuff, all in one codebase, and you have no idea if "." will index table, launch Voyager, or dump core, because everything is dispatched at runtime, it's panic followed by ennui.
replies(1): >>45279960 #
8. AlotOfReading ◴[] No.45272328{3}[source]
C++ would be a very different language if you couldn't use floats:

(NaN + 0.0) != 0.0 + NaN

Inf + -Inf != Inf

I suspect the algebraists would also be pissed if you took away their overloads for hypercomplex numbers and other exotic objects.

9. Animats ◴[] No.45272547[source]
> string concatenation should have its own operator,

It does: |

That character was put in ASCII specifically for concatenation in PL/1.

Then came C.

replies(1): >>45273510 #
10. Animats ◴[] No.45272561{3}[source]
Python managed to totally confuse this. "+" for built-in arrays is concatenation. "+" for NumPy arrays is elementwise addition. Some functions accept both types. That can end badly.
11. jcelerier ◴[] No.45272783[source]
If you've done any university-level maths you should have seen the + sign used in many other contexts than adding numbers, why should that be a problem when programming?
replies(1): >>45273784 #
12. renox ◴[] No.45273510{3}[source]
D (as always) is clever: the operator is ~ So no confusion between addition and concatenation and you can keep | for or.
replies(1): >>45273565 #
13. Defletter ◴[] No.45273565{4}[source]
Question, does that work with other types? Say you have two u16 values, can you concatenate them together with ~ into a u32 without any shifting?
replies(2): >>45273665 #>>45275910 #
14. nicwilson ◴[] No.45273665{5}[source]
It works with arrays (both fixed size, and dynamically sized) and arrays; between arrays and elements; but not between two scalar types that don't overload opBinary!"~", so no it won't work between two `ushorts` to produce a `uint`
15. account42 ◴[] No.45273713[source]
Those languages need a dedicated operator because they are loosely typed which would make it ambiguous like + in JavaScript.

But C++ doesn't have that problem. Sure, a separate operator would have been cleaner (but | is already used for bitwise or) but I have never seen any bug that resulted from it and have never felt it to be an issue when writing code myself.

replies(1): >>45274183 #
16. account42 ◴[] No.45273784[source]
There is usually another operator used for concatenation in math though: | or || or ⊕

The first two are already used for bitwise and logical or and the third isn't available in ASCII so I still think overloading + was a reasonable choice and doesn't cause any actual problems IME.

17. dgb23 ◴[] No.45273792[source]
PHP overloads operators in other ways though.
18. Attrecomet ◴[] No.45273836[source]
Exactly, not allowing operator overload leads to Java hell, where we need verbose functions for calls that should be '+' or similar.
19. CyberDildonics ◴[] No.45273877[source]
But why, where does it become a problem?
20. _flux ◴[] No.45274183{3}[source]
Though then you can have code like "hello" + "world" that doesn't compile and "hello" + 10 that will do something completely different. In some situations you could actually end up with that by gradual modification of the original code..

Granted this is probably a novice-level problem.

21. zbentley ◴[] No.45274942[source]
Regrettably, “intended purpose” is highly subjective.

Sure, << for stream output is pretty unintuitive and silly. But what about pipes for function chaining/composition (many languages overload thus), or overriding call to do e.g. HTML element wrapping, or overriding * for matrices multiplied by simple ints/vectors?

Reasonable minds can and do differ about where the line is in many of those cases. And because of that variability of interpretation, we get extremely hard to understand code. As much as I have seen value in overloading at times, I’m forced to agree that it should probably not exist entirely.

replies(2): >>45275996 #>>45278139 #
22. renox ◴[] No.45275910{5}[source]
No, it doesn't. But I'm not sure that this matter, a sufficiently "smart" compiler understand that this is the same thing.
23. AnimalMuppet ◴[] No.45275996[source]
Depends on what you're trying to understand.

Let's say I have matrices, and I've overloaded * for multiplying a matrix by a matrix, and a matrix by a vector, and a matrix by a number. And now I write

  a = b * c;
If I'm trying to understand this as one of a series of steps of linear algebra that I'm trying to make sure are right, that is far more comprehensible than

  a = mat_mult(b,c);
because it uses math notation, and that's closer to the way linear algebra is written.

But if I take the exact same line and try to understand exactly which functions get called, because I'm worried about numerical stability or performance or something, then the first approach hides the details and the second one is easier to understand.

This is always the way it goes with abstraction. Abstraction hides the details, so we can think at a higher level. And that's good, when you're trying to think at the higher level. When you're not, then abstraction just hides what you're really trying to understand.

replies(1): >>45279988 #
24. lelanthran ◴[] No.45278090[source]
> The first programming language that used overloaded operators I really got into was Scala, and I still love it. I love that instead of Java's x.add(y); I can overload + so that it calls .add when between two objects of type a. It of course has to be used responsibly, but it makes a lot of code really more readable.

The problem, for me, with overloaded operators in something like C++ is that it frequently feels like an afterthought.

Doing "overloaded operators" in Lisp (CLOS + MOP) has much better "vibes" to me than doing overloaded operators in C++ or Scala.

25. wvenable ◴[] No.45278139[source]
> we get extremely hard to understand code

The thing is code without operator overloading is also hard to understand because you might have this math thing (BigIntegers, Matrices) and you can't use standard notation.

26. butterisgood ◴[] No.45279942[source]
I kind of like that OCaml, which I've also not used a great deal, has different operators for adding floats vs integers etc...

Extremely clear at the "call site" what's going on.

27. butterisgood ◴[] No.45279960{3}[source]
Perl was one for me that I always had a little trouble reading again later.

I guess forth as well... hmmm

28. fluorinerocket ◴[] No.45279988{3}[source]
but is it element by element multiplication, or matrix multiplication? I honestly just prefer calling matmul.
29. powerboat9 ◴[] No.45297713[source]
Why is using << for output bad? From what I've seen, it's usually not too hard to figure out that the left hand side is some kind of stream and not a number.