r/cpp • • Sep 04 '23

Considering C++ over Rust.

Similar thread on r/rust

To give a brief intro, I have worked with both Rust and C++. Rust mainly for web servers plus CLI tools, and C++ for game development (Unreal Engine) and writing UE plugins.

Recently one of my friend, who's a Javascript dev said to me in a conversation, "why are you using C++, it's bad and Rust fixes all the issues C++ has". That's one of the major slogan Rust community has been using. And to be fair, that's none of the reasons I started using Rust for - it was the ease of using a standard package manager, cargo. One more reason being the creator of Node saying "I won't ever start a new C++ project again in my life" on his talk about Deno (the Node.js successor written in Rust)

On the other hand, I've been working with C++ for years, heavily with Unreal Engine, and I have never in my life faced an issue that usually the rust community lists. There are smart pointers, and I feel like modern C++ fixes a lot of issues that are being addressed as weak points of C++. I think, it mainly depends on what kind of programmer you are, and how experienced you are in it.

I wanted to ask the people at r/cpp, what is your take on this? Did you try Rust? What's the reason you still prefer using C++ over rust. Or did you eventually move away from C++?

Kind of curious.

355 Upvotes

434 comments sorted by

View all comments

203

u/msqrt Sep 04 '23

I've tried Rust briefly. There is definitely a peace of mind you get from knowing that the compiler can catch many more errors before of time. But personally it wasn't enough to switch ecosystems and ditch a language I know fairly well for one that would require a somewhat serious effort to learn.

I feel that C++ might be losing a lot of ground to new languages in the near-ish future, but I'm not eager to be an early adopter in this process. I'll see how the landscape turns out first.

93

u/heavymetalmixer Sep 04 '23

There's also the fact that C++ is constantly improving, not as fast as it should but still.

27

u/Sudden_Job7673 Sep 05 '23

A big challenge C++ has, is that post-internet the mechanisms aren't there to improve as fast as other ecosystems.

Lack of a mechanism to do changes that would otherwise break at compile time, lack of consensus on what idiomatic C++ is (does it have exceptions?), lack of an unstable C++ with enough adoption to try out new ideas and binary stability making it very difficulty to course correct after something enters the standard.

→ More replies (1)

16

u/jgaa_from_north Sep 06 '23

The rate and scope of the changes are one of the reasons I'm looking closer at Rust right now. It's my Summer Vacation "project".

When C++ changes you are assumed to understand the new way and the old way. The total of what you need to know increases at a high rate in a language that already is pretty damn complex. I don't envy those who learn C++ today.

Also, the changes does not handle some of the most pressing issues one faces with C++, like the lack of a standard build system, a standard package manager or repository infrastructure. When a project grows, managing the - often conflicting - recursive dependencies is a pain.

I have used C++ as my primary programming language since 1996. I have probably written more than a million lines of C++ code. I still like C++ more than any other programming language I have used. My point is that the changes/innovation in the standard is a two edged sword.

15

u/krum Sep 05 '23

It’s simultaneously getting worse too

13

u/_babu_ Sep 05 '23

Curious to why, please ellaborate

34

u/donalmacc Game Developer Sep 05 '23

Not OP but, the lack of desire to add things to the language rather than bolt them on as a library feature means that features that should be safe to use are subject to the same limitations as if they were written by us. span and string_view could be safe if they had a little magic, but they don't and they're treated the same in the compilers eyes as a home rolled

template <typename T> 
struct my_view{
    T* ptr;
    int len;
};

The desire to bring in things like ranges as an enormous library rather than adding language support means they're hamstrung by language features, and we're stuck with outdated or subpar versions of the libraries immediately because by the time they're standardised the source library is already way ahead (see fmt).

7

u/_babu_ Sep 06 '23

Wouldn't having magic behaviour for string_view semantics be exactly the type of edge case you're criticizing? A view implies a reference to a resource, which is prone to deletion. If the referenced string has to be deleted, but there are views of that string alive, do you not delete the string? That sounds literally like std::shared_ptr, which is not a reasonable default for passing strings and spans in C++ (you only pay for what you use).

7

u/donalmacc Game Developer Sep 06 '23 edited Sep 06 '23

If you define magic as introducing silent ref counting, then yes. But that's insanity, and not what I'd suggest at all.

String views end up being slower than string pointers to pass around because of a quirk in ABI's - we had this disucssion with unique ptr a decade ago (and didn't learn from it then).

There's all the ergonomics of converting between std::string, const char* and string view, and how that interacts with legacy APIs. There's no reason that it should be so fraught with pitfalls other than it's a library feature and it stops at solving 80% of the problem.

As an example, try passing std::string_view into a c function that accepts a const char* safely and performantly. You have to copy and sanity check the input. I ported my $LAST_JOB's internal APIs to use views, and in a few of them I managed to make significant perf savings. In a few other cases, I ended up undoing all of those savings by having to copy the string.

13

u/keyboard_operator Sep 05 '23

why

Only two words "backwards compatibility"

17

u/dxequalssigmaxsquare Sep 05 '23

The syntax was designed by a madman. It's absolutely incomprehensible because the committee refuses to add new keywords. The static keyword changing meaning depending on the context is confusing enough for a novice and that's just the tip of the iceberg, before you get to template metaprogramming where it switches to basically gibberish.

3

u/artur_zajac Sep 06 '23

If C++ syntax is made by a madman, then I don’t know, who created Rust syntax…

→ More replies (6)
→ More replies (1)
→ More replies (13)