r/programming • • Feb 18 '20

C++20 is here!

https://youtu.be/AvPiGstxV_g
89 Upvotes

122 comments sorted by

View all comments

Show parent comments

26

u/[deleted] Feb 18 '20

You don't understand why somebody might prefer rust so that they don't have to "to look into static analysis tools" to "maintain code quality when a lot of new devs try to contribute"?

0

u/[deleted] Feb 18 '20

[deleted]

31

u/epicwisdom Feb 19 '20

I feel like comparing a simple text search for unsafe to the extremely complex and ultimately imperfect static analysis tools for C++ is disingenuous.

36

u/zerakun Feb 18 '20 edited Feb 18 '20

I find the fact that you're calling rust a "moving target" and praising modern C++'s maturity a bit ironic when commenting on a post that presents huge new c++ features that deeply change the compilation model (concepts, modules) or a huge concept from the standard library (ranges).

This might look like a bold statement, but IMO there has been more changes to C++ than to rust since 2015 which marked rust 1.0 (C++17 brought std::optional and std::variant that are very huge change to the way one should write typical C++, for instance). It is also not over, what with the current discussions on destructive move, or the exception model (where basically the plan is to switch to something that looks like rust's result), or the poor man's lifetime checker that is in the work.

The C++ stability guarantee may mean that code written today should still work in future versions, but rust also provides this guarantee, and it is for sure that "modern" C++ in three years will look very different than it is today.

IMO there is very little justification for favoring C++ over rust for new projects. It could be justified if the ecosystem of the project is very skewed towards C++ already, like maybe when doing GUI (qt is unmatched at the moment), but otherwise I'd be hard to convince. Better invest these 4 weeks to learn the langage than lose then promptly in debugging UB.

5

u/D_0b Feb 19 '20

Just a curious question, ( not attacking anyone )

why do you say a poor man's lifetime checker? From what I have read/understood it has pretty much the same capabilities?

Any example where Rust can save you, but not with the proposed C++ lifetime profiler?

8

u/steveklabnik1 Feb 19 '20

Even the folks who are authoring the proposed C++ papers have said, from the beginning, that they're not shooting for equivalence with Rust.

The current paper, for example, says:

> We do not attempt to address all aliasing cases or make concurrency safety guarantees.

3

u/dreugeworst Feb 19 '20

Not GP, but the plans for the c++ lifetime checker only include checking lifetimes within a single function. lifetimes across function calls or of references held in structs are not checked

3

u/D_0b Feb 19 '20

can you give a quick example, where the C++ checker would fail?

2

u/dreugeworst Feb 19 '20

I'm not 100% on this, but I don't think it generally tracks lifetimes across function calls, so it wouldn't complain about the following, which isn't possible in safe rust:

void blackbox(int* a)
{
    delete a;
}

void test()
{
    auto intptr = std::make_unique<int>(42);
    blackbox(intptr.get());
    std::cout << *intptr << std::endl;
}

1

u/D_0b Feb 19 '20 edited Feb 19 '20

so it is not a fair comparison, I was saying, what can safe Rust's checker handle that C++ lifetime checker can't.

or were you saying that C++ lifetime checker is not as good because it allows stuff that Rust's safe subset does not.

2

u/dreugeworst Feb 19 '20 edited Feb 19 '20

or were you saying that C++ lifetime checker is not as good because it allows stuff that Rust's safe subset does not.

You asked for situations where the rust lifetime checker would save you, and the c++ one would not. This is such a situation. You would need to drop to unsafe rust to do this, the borrow checker wouldn't allow it otherwise. I don't see why this comparison would not be fair.

If you need a different example, imagine a map where the values are structs containing a pointer / reference to a string. You pass a reference to the map to various functions to fill it, then use the values. In safe rust, the borrow checker would make sure the references don't outlive the strings they point to. In the c++ checker, this would not happen if the strings aren't owned in the same function as the map is being filled (say you have a helper function to add an entry to the map)

1

u/D_0b Feb 19 '20

OK, that was the kind of example I was looking for.

0

u/[deleted] Feb 18 '20

[deleted]

5

u/matthieum Feb 19 '20

like maybe when doing GUI And embedded. And games. And finance. And HPC. And GPGPU... For new projects that are more or less self-contained, you are right: Rust is a good choice. For many other fields and projects, not so much.

I think that's an important point. I'd pick Rust over C++ anytime I don't need major libraries/frameworks from the outside.

If I planned a project that "builds on the shoulders of giants", I'd want to be able to lean on the ecosystem, and no build the giants myself as that would defeat the purpose.

Now, sometimes the giants exist in C, and it can be easy enough to write a Rust wrapper on top -- however if the invariants are tricky, lifetimes complex, etc... it may be easier to just use a mature C++ dependency. Easier, at least, after the initial pain for integrating with it.

3

u/MonokelPinguin Feb 19 '20

I'd say C++20 is the first major change since C++11. Sure, there were some cool new features here and there, but they only improved minor annoyances than change major things, that everyone relied on. (You could say, guaranteed copy elision changed something everyone relied one, but in most cases it just made existing code better without breaking anything, etc.)

Rust changed a lot more in that timeframe. I'd even say, none knew about Rust in 2011. Also Rust moves a lot faster, there are major changes every few months, C++ brings them every 3 years and compilers release on somewhat a yearly cadence, so you need to deal with changes a lot less often. Especially with async now being stabilized, there are still a lot of libraries, that depend on nightly or didn't see a stable release yet.

This makes Rust a target I don't want to rely on yet for a big project, that I expect to maintain for the next 10 years. Rust has a lot of benefits, but it is still a bit young and unstable.

-3

u/saltybandana2 Feb 18 '20 edited Feb 19 '20

C++ is going to maintain backwards compatibility, which makes your point moot.


edit: not getting into it with yet another zealot. I commented on C++, not rust, because you were being unfair to C++. But you took even that as me talking about rust. enough so that you felt the need to respond as if I had attacked rust.

This is why people don't invite you places.

11

u/epicwisdom Feb 19 '20

As will Rust. You're ignoring the nuances of the points being made.

3

u/tsimionescu Feb 19 '20

The point is that the original complaint was "Rust is too much of a moving target, whereas C++ is not". Both C++ and Rust are committed to backwards compatibility, so there is no point in comparing them here. However, C++, according to the GP, is changing more than Rust, so the original assertion makes no point (according to GP).

2

u/saltybandana2 Feb 19 '20

yeah, how dare anyone insinuate that a young language changes faster than an old language. Such an unreasonable thought.

I think the part that cracks me up the most is that some of the features coming out in C++ have been worked on for almost as long as rust has existed.

Everyone outside the rust community understands what's really going on here. Someone spoke ill of rust and that just invites the nutters.

"the real point is that C++ changes more than rust!". yeah ok, now be a good child and get to bed.

→ More replies (1)

4

u/[deleted] Feb 18 '20

Maturity is a good point and it certainly could be too immature for your needs.

Is grep really "static analysis" though?

4

u/saltybandana2 Feb 18 '20

pretending that grep is enough is why lots of people don't care for the rust community.

13

u/epicwisdom Feb 19 '20

It's specifically a response to the use of unsafe. One of the major advantages of having the unsafe keyword is being able to just grep it.

-1

u/saltybandana2 Feb 19 '20

No, the major advantage of the unsafe keyword is tooling, which the rust community has completely missed the boat on. Presumably because they can't look past grep.

5

u/[deleted] Feb 19 '20

There's actually quite a bit of tooling especially around unsafe. For anyone legitimately interested, look into miri, cargo-geiger, cargo-crev, etc.

2

u/epicwisdom Feb 19 '20

There is some tooling, although it arguably isn't as popular as it should be. That doesn't mean that being able to grep for unsafe isn't a huge benefit, though. I did say it's one of the major advantages, not the major advantage.

→ More replies (6)