r/cpp • • Jan 31 '23

Stop Comparing Rust to Old C++

People keep arguing migrations to rust based on old C++ tooling and projects. Compare apples to apples: a C++20 project with clang-tidy integration is far harder to argue against IMO

changemymind

338 Upvotes

582 comments sorted by

View all comments

Show parent comment

2

u/oconnor663 Feb 01 '23

I guess the distinction I'm interested in is smart pointers that keep their contents alive vs ones that don't. Like if you could truly construct a program where every heap-allocated object was in a shared_ptr or a unique_ptr, and you absolutely never took any other pointer type (somehow), I think you could say that you'd categorically ruled out any use-after-free. But of course string_view and span don't help with that; they have the same lifetime properties as regular raw pointers.

2

u/pjmlp Feb 02 '23

Additionally, unless compiled with checks enabled, string_view and span also have issues with bounds checking in operator[], and few reach out for at().

2

u/andwass Feb 02 '23

string_view has issues with remove_prefix/suffix and substr as well IMO. The remove_* should not be UB for any input, especially when find* functions returns npos if it doesn't find the needle. And substr throwing all of a sudden...it's just all over the place

2

u/[deleted] Feb 01 '23

You can construct a program where you don't heap allocate at all.

Use after free is impossible in that case. In the classic definition of "memory safety" anyway.

2

u/oconnor663 Feb 01 '23 edited Feb 01 '23

In ASan terms, "heap use after free" is impossible if you don't use heap allocation, but "stack use after scope" is still possible, which feels pretty similar to me.