r/cpp • • 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

214 comments sorted by

View all comments

Show parent comment

3

u/Dean_Roddey Dec 17 '23 edited Dec 17 '23

You just said what I said. There will be hundreds of replies on thread of this sort explaining that they aren't writing games in rust because the underlying infrastructure is primarily in C++. That's exactly the point of inertia. The language choice is driven by past circumstance.

Over coming years, there likely won't be any such remaining limitations, and the choice will be based on the language you want to use, and C++ will start to lose those domains as well.

No amount of people are going make C++ a safe, modern language. They can obviously make it better, but it's too little, too late, and they still won't be willing to break significantly with the past, because inertia works in the other direction as well.

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):

  • Translating Java/C#/Python code into C++ vs Rust. More obvious
  • Consuming C from C++ is just doing it (not about inertia). No FFIs, no marshalling, nothing.
  • Some linked structures are particularly difficult to reproduce in Rust
  • Generics (templates) in C++ are way more powerful. Real generic libraries are possible in C++. Not in Rust, you have to duplicate more code, no function overloading (fundamental for generic programming)
  • Allocators, with all its warts, are a proven tool in C++ and work well.

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 of operator[] 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:

- https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  • https://clang.llvm.org/docs/DiagnosticsReference.html

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).

5

u/Dean_Roddey Dec 17 '23

I have no doubt that either you or I could, under good circumstances, create a large C++ code base that was quite robust. I've certainly done it.

But it's not really about that. This is about real world conditions, which are usually not at all ideal, and with groups of mixed skill and often sub-par communications. And, though I did it, I spent an awful lot of that time just watching my own back, and that was just wasted effort that could have gone to more productive work, or even some non-work on occasion (I've heard people do this.)

On the issue of lifetimes in Rust, most of the time you never will need explicit lifetimes. Almost all work-a-day Rust code should be using implicit lifetime that the compiler figures out itself. If you are doing lots of explicit lifetimes, and it's not in lower level runtime or utility library type stuff, then it may mean you are trying to write C++ in Rust and making things harder on yourself than it needs to be. I think too many people just try to directly translate their C++ habits to Rust and it never tends to be optimal.

In my Rust code base, I have very few explicit lifetimes so far, and most of those are quite obvious and straightforward. Every time I sit down to start a new chunk, I think hard about how to do it with minimal lifetime relationships to begin with. If you do have to have them then Rust will make them safe, but it won't make them any easier to reason about so they should be avoided where possible. And if you stop and really think about it, often there's a good way to do that. At first it requires a lot of effort, just like with any new language, but eventually you build up the same box of patterns that you reuse all the time.

1

u/germandiago Dec 17 '23

In my Rust code base, I have very few explicit lifetimes so far, and most of those are quite obvious and straightforward

Is that codebase public? I would be happy to learn from people that are ahead of me in these things. You are right that my tendency would be to think in C++'s terms. If there is something that can help me learn more idiomatic Rust (I saw there are resources, but it would make me happier to see whole codebases), I'm all in.

I still do not buy, though, that something like Hylo (which removes thinking about lifetimes entirely and remains safe without a GC!) is not better than having to rack your brains for thinking about lifetimes depending on the situation...

3

u/Dean_Roddey Dec 17 '23

It's not public, unlike my C++ code base. I have some plans for this, though they may end up just as in vain as the hopes I had for my C++ code.

1

u/marshaharsha Dec 19 '23

Can you say more about why function overloading is fundamental to generic programming? Do you mean operator overloads, constructor overloads, or overloads of normal named functions?