Live data from Hacker News

Is C++ Doomed?

tednesday.wordpress.com

61–70 of 168 posts

Re: Is C++ Doomed?

#61
I agree that C++ is a mess of a design, but I don't think this article quite gets how. RAII and move semantics are two of C++'s best features, and the ones that any replacement - like Rust - will copy.

To me, bigger flaws with C++ are that it's too big. The language is bloated with multiple features that accomplish similar things, but each with their own odd corner cases and interference. Anytime there's an issue from a language component, the solution is to tack another language component on top of things. See for example the progression of Macros -> Classes -> Templates -> Constexpr -> Concepts. Each tried to fix flaws in the previous design, but each introduction made the language more complicated, not less.

> But exceptions come at a performance cost.

I mean it's slow to throw, but non-throwing code paths are hardly affected. Typical implementations store the exception unwinding stuff off to the side, so the only cost is binary size.

> That means you need to write code that wraps every possible resource in RAII logic.

Consider using scope guards. You can write them directly in your code, without a separate class per resource.

    int* ptr = malloc(...);
    auto guard = defer([&]{ free(ptr); });

Re: Is C++ Doomed?

#62
post #45

I am very confused. Some of these ramblings are just plain wrong, like > But exceptions come at a performance cost. No they don't. An exception that isn't thrown costs nothing. Also > Let’s say I want to know if a constructor failed. I have two options, one is to pass in an in-out parameter, the other is to the throw an exception. Using a factory function instead is a third option that avoids this. Besides, this is a…

To add to "Let’s say I want to know if a constructor failed."

Constructors are simply not meant to fail. You should never do something like io in a constructor. For everything more complicated than setting some fields a factory is better suited.

Re: Is C++ Doomed?

#63
post #14
post #9

The commentary is good, but the conclusion is unsupported. These are annoying issues, but doom they do not make.

Have you heard the phrase "death by 1000 cuts"? That's what programming in modern C++ is like. I agree they don't quite make the case for "doom" but it's still not a pretty picture.

This image is mostly painted by people who forgot to stop.

By that I mean stop using every feature and detail because it's there.

C++ can be used like a much better C (smart pointers, references, const-correctness, generics via templates) and most of the complications can be ignored that way.

Premature optimisation is another big factor. If there's no measurable performance bottleneck, things like move-semantics don't even matter in practise for example.

Re: Is C++ Doomed?

#64
post #3

Gonna be a long while before safety and certification bodies for industries like aerospace move away from C++. All the tools for the past few decades focus on C++ as the main systems language in these cases. Rust is great as a spiritual successor with more stable performance (in general) but the institutional inertia with C++ is strong.

In this aerospace company, we use C, no C++ allowed, to avoid all the extra footguns that get in the way of safety and certification. Rust will probably be the next choice on the scene, but I expect it'll take as long to come as Ada took to go.

> Rust will probably be the next choice on the scene

I'm not sure about it.

I think Rust might be hard to certify for aerospace usage.

Not because it's bad, but because it's still complex. Just a lot of negative side effects of complexity are contained due to the compiler checks and design.

But that makes it harder to certify as I can tell.

Re: Is C++ Doomed?

#65
As someone who uses both Rust and C++ frequently with a good understanding of both languages, I can say that Rust is for the mostpart more pleasant to work in, but Rust's handicap is that it makes working in raw pointers when you really have to quite painful.

If you need full raw pointers with aliasing mutable pointers, the fact that fn drop() takes a &mut T is a problem that can only currently be worked around with fugly hacks, like nesting your data type in another pointless structure, not to mention the giant pile of ambiguous UB that you risk whenever pointers and references interact that could so easily be avoided if Rust would just implement an -fno-strict-aliasing switch, but the devs refuse on what appears to be ideological grounds. The result is that for unsafe Rust, you get about the safety level of C much of the time, because RAII and many language facilities are not safe to use. Unsafe Rust is currently so dangerous that I feel much safer reaching for C++ for some of it. Mutable reference uniqueness, I think, should be enforced by the borrow checker and NOT by pain of inescapable possible undefined behavior at the assembler level.

Now for C++.

C++ is lacking a LOT of useful safety features that Rust comes with baked in, such as Mutexes owning their data, the borrow checker, a good, fast atomic reference counting type, and things that translate to more safety indirectly, like algebraic enums, assignable and movable arrays, tuples built into the language, etc. Generics in Rust, however, are significantly more painful to write due to the lack of duck typing. Perhaps the Rust devs felt that duck typing was too "dangerous" for templates, but because they're statically evaluated, these issues almost never materialize, and so in practice duck typed templates are almost always superior to Rust's generics.

C++ is definitely more mature and pragmatic as a language, but Rust is probably better for systems programming overall, at least if they fix their goddamn unsafe UB stuff.

As for move semantics, that book isn't for "what you MUST do to keep things semi-safe!" though it contains some stuff to that effect. It's more about optimization and tricks from what I can see. Move semantics take a little while to wrap your head around, but once you know the basics of C++'s move semantics, you can pretty much figure out what's going to happen by looking at a class' definition briefly.

Re: Is C++ Doomed?

#66
post #45

I am very confused. Some of these ramblings are just plain wrong, like > But exceptions come at a performance cost. No they don't. An exception that isn't thrown costs nothing. Also > Let’s say I want to know if a constructor failed. I have two options, one is to pass in an in-out parameter, the other is to the throw an exception. Using a factory function instead is a third option that avoids this. Besides, this is a…

> The author seems to look for something like Go instead

But in Go you also have to write exception safe code, something the author complained about.

Before someone says "but you're not supposed to throw or catch in Go": the std http server swallows exceptions thrown in handlers, and fmt.Print does for String callbacks too.

And before someone says Go doesn't have exceptions: It does, 100%, in all but name.

I actually find C++ to be way more designed, though through, and consistent than Go.

That's not to say that Go isn't a vastly simpler language. It is. And C++ has way more "just because you can doesn't mean you should".

Re: Is C++ Doomed?

#67
C++ is doomed, but it'll add so many features to mimic the cool new things that it'll cease to be C++.

TFA: "That is, of course, until you realise the design philosophy that is being followed. They literally just implement whatever the current fad in programming is."

Re: Is C++ Doomed?

#68
post #57
post #3

Gonna be a long while before safety and certification bodies for industries like aerospace move away from C++. All the tools for the past few decades focus on C++ as the main systems language in these cases. Rust is great as a spiritual successor with more stable performance (in general) but the institutional inertia with C++ is strong.

Aerospace industry? C++? Frightening. I thought Ada is prevalent in this space.

I'm not in the industry but from what I know it's a split between Ada/C/C++. More space related applications tend to be C/C++ (satelites, rovers, etc.) while the aero-side uses more Ada (Boeing, Airbus etc.).

Re: Is C++ Doomed?

#69
post #45

I am very confused. Some of these ramblings are just plain wrong, like > But exceptions come at a performance cost. No they don't. An exception that isn't thrown costs nothing. Also > Let’s say I want to know if a constructor failed. I have two options, one is to pass in an in-out parameter, the other is to the throw an exception. Using a factory function instead is a third option that avoids this. Besides, this is a…

> An exception that isn't thrown costs nothing.

It costs binary size for unwinding tables, and in some cases it is an optimization barrier.

Re: Is C++ Doomed?

#70
post #8
post #3

Gonna be a long while before safety and certification bodies for industries like aerospace move away from C++. All the tools for the past few decades focus on C++ as the main systems language in these cases. Rust is great as a spiritual successor with more stable performance (in general) but the institutional inertia with C++ is strong.

What's the state of Rust/C++ interop currently? Naively it seems like allowing Rust in C++ projects could be a reasonable way forward, but I haven't tried it personally yet.

I think you're implicitly saying that Rust solves the problems in the article, and so migrating C++ projects to use Rust is a path forward. However, the blog author's first example was implementing a "contiguous circular queue", so you should try to do that in Rust. Of course Rust already has VecDeque, but then C++ already has std::deque too (although not promised to be contiguous). So the exercise is to implement your own, from scratch, without just wrapping a type that does the dirty work for you.

If you look closely at the implementation details for VecDeque, you'll find a lot of complexity you might not expect. To do it efficiently and correctly, you need to work with unitialized memory. So there will be unsafe blocks in there. A VecDeque is built of a RawVec, which uses a Unique, which finally has a pointer, but also contains a magical PhantomData. Look at the code [0] [1] [2] [3], look at the implementation details, read the comments, and assess for yourself if it's easier or harder than C++.

Later in the article, he talks about exception safety. For something like a container, the most likely exceptions are that you've run out of memory, or you can't move/copy/construct an item in the container. I'm honestly not sure how Rust's builtin containers handle these problems. (Panic?) But if you're comparing the two languages, the apples and oranges matter.

All of this to say that Rust is not simple either. You can program Rust by using its standard collections, and you can do the same with C++. If you try to implement those collections, say for learning/teaching data structures, both languages seem painfully complicated to me.

[0] https://doc.rust-lang.org/src/alloc/collections/vec_deque/mo...

[1] https://doc.rust-lang.org/src/alloc/raw_vec.rs.html#52

[2] https://docs.rs/ptr/0.1.0/src/ptr/lib.rs.html#39-47

[3] https://doc.rust-lang.org/src/core/marker.rs.html#679

Post reply on HN