←back to thread

In Defense of C++

(dayvster.com)
185 points todsacerdoti | 3 comments | | HN request time: 0.658s | source
Show context
monkeyelite ◴[] No.45270587[source]
The complexity argument is just not true. You do have to know this stuff in c++, you run into it all the time.

I wish I didn’t have to know about std::launder but I do

replies(2): >>45270919 #>>45271366 #
ryao ◴[] No.45270919[source]
I feel like C++ is a bunch of long chains of solutions creating problems that require new solutions, that start from claiming that it can do things better than C.

Problem 1: You might fail to initialize an object in memory correctly.

Solution 1: Constructors.

Problem 2: Now you cannot preallocate memory as in SLAB allocation since the constructor does an allocator call.

Solution 2: Placement new

Problem 3: Now the type system has led the compiler to assume your preallocated memory cannot change since you declared it const.

Solution 3: std::launder()

If it is not clear what I mean about placement new and const needing std::lauder(), see this:

https://miyuki.github.io/2016/10/21/std-launder.html

C has a very simple solution that avoids this chain. Use structured programming to initialize your objects correctly. You are not going to escape the need to do this with C++, but you are guaranteed to have to consider a great many things in C++ that would not have needed consideration in C since C avoided the slippery slope of syntactic sugar that C++ took.

replies(3): >>45271873 #>>45272878 #>>45276958 #
jcelerier ◴[] No.45272878[source]
But the c++ solution is transparent to the user. You can write entire useful programs that will use std:: containers willy-nilly and all propagate their allocators automatically and recursively without you having to lift a finger because all the steps you've mentioned have been turned in a reusable library, once.
replies(2): >>45273189 #>>45276659 #
1. nxobject ◴[] No.45273189[source]
I'd file that in the category of "what I can't recreate, I can't understand".
replies(1): >>45274302 #
2. account42 ◴[] No.45274302[source]
With that argument you could discard JavaScript because V8 is hard to understand.

C++ giving you the ability to create your own containers that equal the standard library is a bonus, it doesn't make those containers harder to use.

replies(1): >>45280146 #
3. nxobject ◴[] No.45280146[source]
That's a false comparison. There's a huge difference between a standard container library, and the combination of a (a) best-in-class byte code interpreter with a (b) caching, optimizing JIT, supported by (c) a best-in-class garbage collector.

I would argue that it's reasonable to say that creating a robust data structure library at the level of the STL shouldn't be that arcane.