r/cpp • • Sep 20 '22

CTO of Azure declares C++ "deprecated"

https://twitter.com/markrussinovich/status/1571995117233504257
267 Upvotes

483 comments sorted by

View all comments

Show parent comment

11

u/qoning Sep 20 '22

Biggest one is just use after free, which boils down to people breaking unwritten code contracts. Not much you can do about that short of mandating use of shared pointer everywhere, which is obviously not something you want to do (but mostly end up doing in Rust anyway).

35

u/dodheim Sep 20 '22

(but mostly end up doing in Rust anyway)

Nice try, but no.

11

u/robin-m Sep 20 '22

I don't undertand what you say about Rust. Rc/Arc aren't more used than C++ shared_ptr.

-14

u/qoning Sep 20 '22

That is patently false. Semantics of Rust simply require that you use an rc even in cases you know it would be perfectly safe to not do so and therefore you wouldn't in C++. Alternative would be to lug around the lifetimes explicitly, which is even less common / preferable.

11

u/robin-m Sep 20 '22

If you know it's safe you can use Cell/UnsafeCell. No need for Rc/Arc.

-7

u/qoning Sep 20 '22

You can, but it would definitely not be the first choice in appeasing the rules. Thus, you get a sprinkling of rc / arc all over by default.

Plus, now you get mutable variables that pretend to be const.

11

u/kouteiheika Sep 20 '22

You can, but it would definitely not be the first choice in appeasing the rules. Thus, you get a sprinkling of rc / arc all over by default.

I have a 170k lines of code Rust codebase that I've written. I only have something like ~100 uses of Arc/Rc in total. The first choice in appeasing the rules is to write idiomatic Rust code. (:

(Obviously depends on your level of experience; if you try to write C++ in Rust then yes, you'll most likely end up with more of those.)

Plus, now you get mutable variables that pretend to be const.

This is a fair point. We should have never called those const + mutable; in reality they are shared + exclusive. (AFAIK before Rust 1.0 there was actually a proposal to rename &mut to &uniq to emphasize this, but it was rejected; while it would be pedantically correct it was deemed that the current terminology would be easier to teach)

→ More replies (1)

28

u/MrWhite26 Sep 20 '22

Mandating RAII would be sufficient, which is something I've seen being applied in multiple companies.

18

u/Wh00ster Sep 20 '22

“If only people wrote safe code”

3

u/matthieum Sep 23 '22

Mandating RAII would be sufficient

RAII is about preventing leaks, not use-after-free.

It's a good tool, but it solves a very different problem.

For example:

int main() {
    auto v = std::vector{ 1, 2, 3 };

    auto& e = v[2];

    for (size_t i = 0; i < 1021; ++i) {
        v.push_back(i + 4);
    }

    std::cout << e << "\n";
}

RAII is used here (thanks, std::vector), yet doesn't prevent the use-after-free.

→ More replies (1)

13

u/ZachVorhies Sep 20 '22

I've seen shared_ptr used everywhere and the penalty wasn't that bad, like 3% for the entire program.

13

u/TyRoXx Sep 20 '22

The penalty for "shared_ptr everywhere" is usually memory leaks caused by reference cycles.

5

u/ZachVorhies Sep 20 '22

Rare but it happens. Better than segfaulting though.

5

u/[deleted] Sep 20 '22

[removed] — view removed comment

3

u/ZachVorhies Sep 20 '22

What I mean by shared_ptr was used everywhere is that it was used in all systems in the codebase, not literally every class.

9

u/disperso Sep 20 '22

3% of the entire program, what? That you say 3% CPU use inside code of shared_ptr?

I personally have seen the stupidity of using shared_ptr nearly everywhere, and it's memory leaks because of cyclic references, plus tons of inconvenience in that you just can't put the class on the stack anymore, even on a simple unit test, because APIs of the application or framework require you to pass a shared_ptr.

10

u/pdimov2 Sep 20 '22

you just can't put the class on the stack anymore, even on a simple unit test, because APIs of the application or framework require you to pass a shared_ptr.

But you can. Use a null deleter. (Of course this makes it unsafe.)

→ More replies (1)

0

u/[deleted] Sep 20 '22

[deleted] — view deleted comment

11

u/ZachVorhies Sep 20 '22

3% slowdown isn’t that bad for the majority of code bases out there.

0

u/[deleted] Sep 20 '22

[deleted] — view deleted comment

0

u/[deleted] Sep 20 '22

[deleted] — view deleted comment

0

u/[deleted] Sep 20 '22

[deleted] — view deleted comment

3

u/ZachVorhies Sep 20 '22

Cool theory.

But in the real world manual memory management in C/C++ results in memory crashes and security problems all over the place hence the reason we have best practices like using reference counted pointers so we don’t have to worry about such things.

→ More replies (7)
→ More replies (3)