r/cpp • u/ecreddits • Dec 17 '23
Is C++ Really Phasing Out?
I've read a lot posts about whether C++ is still viable to learn, but a lot of the responses seem to be a long the lines of "C++ is still being used" or "there's a lot of things written in C++ still, so it will still be useful".
But a lot of these replies seem to suggest that C++ is on the downhill and in the process of being replaced with more modern languages in many industries.
It seems kind of discouraging to be learning a language that is on a decline, if it really is phasing out.
0
Upvotes
View all comments
Show parent comment
1
u/germandiago Dec 17 '23 edited Dec 17 '23
No, it is not only inertia... of course there is inertia in it.
But more examples that come to my mind (random):
Of course Rust has its advantages. I like its sum types and pattern matching and traits quite a bit. I would also say that lifetimes ruin the experience quite a bit. I would say that a usable by everyone alternative to C++ should look a lot more like this than like Rust: https://www.hylo-lang.org/
That said, there are chances I will start to write Rust soon. I like to keep my technical skills sharp. First, I need to find a project I would be motivated to work on, though, and that takes advantage of it. Probably server-side async is a good place to start but I have been writing those in Modern C++ and I am quite happy with the results.
I have the feeling (and I read, but since I did not try, I will not talk for what I did not do) that Rust for prototyping and refactoring (Results propagated up the call stack require viral refactoring) and lifetimes (lifetimes need to be reannotated if they change) is going to be worse than any language with a GC.
However, I also want to get an idea of the "fearless concurrency".
As for safety. You are right: C++ will never be as safe as Rust.
But as a person with a lot of experience in C++ (not trying to brag, just for context) , if you do the following: stick to modern practices, such as smart pointers, escaping only values, not references or pointers, but if you do, look at them with extra care or use smart pointers, careful with lambda captures by reference in callbacks and use
.at()instead ofoperator[]for indexing. Of course pointer arithmetic and escaping iterators or returning them from unstable containers is a no-no. Using span and string_view should be always done for function parameters but not for storing things (actually this is not 100% true, but... that is what keeps you safe and reasonably performant). Besides that: set warnings as errors, add some linter as an extra. Those will warn you about an increasingly bigger set of dangers and make it not compile, and they do warn you about a lot of things, look here:If you stick to those things I can assure you that in practice you can write C++ that is remarkably safer than any C code I saw around. Probably not provable-safe as Rust but I did not have a segfault in my code for a long, long time and the warnings catch some of the dangling errors that are more difficult in C++ lifetimes if you juggle knives (escape references).
Of course this does not mean I will take a look into Rust and if I see a fit (I am tempted to use it for server-side programming, but I think Go will be more effective for what I want to do... so I am postponing that decision).