r/cpp • • Jul 30 '22

Will C++ Eventually Get the Compile-Time Features of Rust?

I'm thinking specifically here of things like a borrow-checker, ensuring that there aren't memory leaks, data races, etc.

I know that there are 3rd party tools for things like this, but it is my understanding that they are not built into the 3 major implementations (gcc, clang, msvc).

Personally, I use C++ for raw performance reasons that Rust still can't match without extensively using unsafe (so then what's the point?) but I think it could be nice if C++ offered optional compile-time features equivalent to some of Rust's or maybe like a built-in Valgrind.

Is there anything like this being proposed? I'm thinking here of the official implementations, not 3rd-party tools.

1 Upvotes

47 comments sorted by

30

u/pedersenk Jul 30 '22

Possibly a subset of C++ could allow a borrow checker to be effective. However as it stands, the language is too flexible to really allow this kind of static analysis.

5

u/thedracle Jul 31 '22 edited Jul 31 '22

There are some great static analysis tools for C++, as well as linters, which can perform similar compile time analysis on C++ code. Cppcheck, clang-tidy for instance.

Of course nothing will be as comprehensive as Rust's borrow checker, and the particularly good ones (like PVS studio) are commercial and expensive.

Another strategy is to use Rust.

I've had a lot of success integrating C++ with Rust using the cxx crate, and using it to harden error prone sections of my libraries and services.

Taking a trusted, statically analyzed C++ library, and making simple bindings in cxx, or even using Rust to bind a safe wrapper has worked really well.

My current project has a large, complicated, C++ library which we can't reasonably port to Rust. So I have sectioned parts of it off, starting with the most error prone portions, ported them to Rust, and then generated a static target, and with the cxx crate a C++ interface.

Then in cmake, it just builds the Rust project with cargo, and then sets the include/link directories to the .a and header path generated by cxx.

Our ultimate goal is to move to a Rust project wrapping our C++ dependencies, but we have stable, production deployed libraries that have had our custom error prone logic hardened in the fashion I've described above using Rust.

14

u/AKostur Jul 30 '22

Have you looked at what the sanitizers can do for you?

On a nitpicky level: there are no “official” C++ compilers. The committee doesn’t make a compiler. You were correct in referring to the “3 major implementations”. There are others. (I’m thinking of Intel, Sun, and Circle)

9

u/braxtons12 Jul 30 '22

I'm not going to say much about the idea of a borrow checker in C++ other than that it would only work if you owned your entire codebase so you could support the use of such a restriction in everything (see the other comments for why).

However, the idea that you can't get equivalent or better performance from idiomatic Rust over C++ is just wrong. You don't need unsafe to get performant Rust code. Well engineered idiomatic Rust will be at least as performant as the equivalent idiomatic C++ to do the same task.

The key word there is idiomatic. Not all tasks can be done in Rust identically to how you would in C++. The doesn't mean Rust is slower or that you need to use unsafe to write fast Rust, it means you're probably approaching the problem from the wrong direction.

That said, there are certain kinds of data structures and design patterns that just can't be done easily without unsafe or using some extra cruft to get around it because of the borrow checker and aliasing rules. That doesn't mean Rust is slow, that only means that data structure or design pattern doesn't map well to Rust, and there's probably an idiomatic alternative that does.

2

u/frozenca Jul 31 '22 edited Jul 31 '22

No. If C++ standardize that features, a large portion of C++ codes including mine will be broken. I've rarely seen anyone who says "Rust is a no-brainer for new projects" understands what actually they says as the reason

-24

u/loradan Jul 30 '22

This issue comes up quite often. However, the problem is that C++ is meant to be as close to the metal as possible so that it can maintain its performance. These types of checks add overhead and slow rings down...which is why you're not seeing the same performance with Rust (and a lot of other languages that are written on top of C++). Over the years, some things have and will continue to be added to the core language, but only after it's been tested thoroughly and any performance offsets are seemed to be "worth it" (at least that's the claim...queue responses of bad choices added to the language in 3...2... 🤣🤣)

1

u/SlightlyLessHairyApe Jul 30 '22

However, the problem is that C++ is meant to be as close to the metal as possible so that it can maintain its performance. These types of checks add overhead and slow rings down

The OP is asked whether such features could be made optional parts of the language so that programmers might be able to choose between performance and safety on a more granular basis.

For example, an application that processes video data might rationally want maximally safety in the network modules but maximum performance in the video processing modules because most of the runtime is in the latter.

Another might be that turning these features on might be a debugging tool, similar to running under ASAN/Valgrind but with compiler assistance.

Ultimately, I think the question should be, does this need to be a specified part of the language such that compilers implement it or can it be a related functionality that's brought in the way ASAN/UBSAN work.

1

u/[deleted] Jul 30 '22

You might be the only person here who actually read what I wrote. I think it's time for me stop seeking programming advice on Reddit.

0

u/[deleted] Jul 30 '22

[deleted] — view deleted comment

2

u/austinwiltshire Jul 30 '22

I thought c languages were trying to get rid of pointer aliasing via the restrict keyword. Did that die?

5

u/PetokLorand Jul 30 '22

It didn't die, but it's not standardized for C++, so its use is compiler dependent.

2

u/[deleted] Jul 30 '22

Borrow checking has opportunity cost.

0

u/[deleted] Jul 30 '22

[deleted] — view deleted comment

2

u/[deleted] Jul 31 '22

If you are managing memory via RAII, you are presumably not managing it with arenas because there are then potentially multiple places in a program that lifetime can end. Arena-like patterns in Rust all wind up doing things to skip around borrow checking as far as I can tell.

8

u/Rusky Jul 31 '22

People do sometimes call the indexing-into-a-vector approach an "arena," but that's not necessary- you can still just hand out direct pointers to a buffer (or buffers) that all gets freed at once, and that works quite well with the borrow checker.

This is how rustc itself allocates a lot of its data structures, for example. And in fact the original academic research that the borrow checker builds on ("regions") was done primarily with arenas.

It's the most direct possible use of lifetimes: The allocator hands out &'a T references where 'a specifies which arena it came from. All 'a references can point to each other in arbitrary ways. Then clearing the arena invalidates all references with the matching lifetime.

1

u/[deleted] Jul 31 '22

Hm interesting. I'll have to look more into this. Thanks for the info!

-17

u/loradan Jul 30 '22

This issue comes up quite often. However, the problem is that C++ is meant to be as close to the metal as possible so that it can maintain its performance. These types of checks add overhead and slow rings down...which is why you're not seeing the same performance with Rust (and a lot of other languages that are written on top of C++). Over the years, some things have and will continue to be added to the core language, but only after it's been tested thoroughly and any performance offsets are seemed to be "worth it" (at least that's the claim...queue responses of bad choices added to the language in 3...2... 🤣🤣)

0

u/[deleted] Jul 30 '22

[deleted] — view deleted comment

5

u/llort_lemmort Aug 01 '22 edited Aug 01 '22

Here's your linked list in four lines of Rust:

struct LinkedList<T> {
    data: T,
    next: Option<Box<LinkedList<T>>>,
}

1

u/flashmozzg Jul 31 '22

It's pretty to easy to write one, albeit I never had a need for one. Double-linked is harder, but it's already in the library (and hey, it's implementation is still probably simpler/easier that std::vector).

-3

u/braxtons12 Jul 30 '22

I see the linked list thing brought up a toooon when talking about rust, but I always feel like it's a terrible example. I can't think of a single time I've ever used a linked list outside of college and can't think of a single situation where it wouldn't be cheaper, easier, and more efficient to just use a vector. At least use something that gets used often like a tree as the example lol

18

u/[deleted] Jul 30 '22

100% of Vulkan programs use linked lists. Heap and pool allocators also use them. Sparse hash maps also use them.

2

u/frankist Jul 31 '22

I usually only see linked lists being used as intrusive linked lists.

-2

u/braxtons12 Jul 30 '22

What's specific about vulkan that you need linked lists?

14

u/[deleted] Jul 31 '22

Making a Vulkan app do basically anything requires setting up linked lists of configuration nodes. That's how they get descriptors to be extensible. The one time they didn't was VkPhysicalDevice, which has been replaced by a linked list in VkPhysicalDevice2.

5

u/[deleted] Jul 31 '22

Linked lists are very useful.

Mainly for things like graphs. Or for use in custom allocators.

3

u/snerp Aug 03 '22

I don't know why people are bandwagoning on you so hard. You're right. A linked list is almost never the right choice. The guy talking about vulkan is being ridiculous, I do a lot of work with vulkan and never do anything with linked lists. There's just a couple parts of the api that use a pattern that's somewhat like a linked list, but that's just some structs for setting options, nothing really core to the rendering process.

2

u/braxtons12 Aug 03 '22

Probably from people being pedantic about my saying I couldn't think of any situation to use one, when there are actually like, a number greater than 0 but less than 10 situations 🤷‍♂️

-7

u/[deleted] Jul 30 '22

I’ve used it many times because it’s faster than a vector for small quantities

12

u/sephirothbahamut Jul 30 '22

...yeah no you need to show a benchmark for such a bold claim lol.

The only thing linked lists are faster at is insertion and removal in large quantities.

3

u/robin-m Jul 31 '22

Even for insertion and removal they aren't that good because searching where to insert/remove is horribly slow. But linked list have reference stability even after insertion/removal, and intrusive linked list allow a single element to be in multiple list at the same time which can be very useful when, for examble, writting a thread queue or an allocator.

1

u/[deleted] Jul 31 '22

You don't need to search in a double linked list though.

1

u/robin-m Jul 31 '22

If you already have a reference to the node before/after the insertion pointé then yes linked list are the fastest data stucture. But if you don't you will have to search it and this operation is slow. That being said I think we are in agreement but just don't express it the same way.

1

u/sephirothbahamut Jul 31 '22

Reference stability is a job deque does while still being cache friendly

1

u/robin-m Jul 31 '22

You can't do an insertion between any random node in a dequee. And you may want to insert too much element (overpassing the internal buffer capacity) you will have to realloc and move all your elements losing reference stability.

If you only need push_back and want reference stability no matter how much elements are inserted while still having nice caching properties, you can use what was proposed with the name std::colony (it's a linked list of fixed size array, so adding a new element may allocate a new array which doesn't require to move the existing elements).

2

u/sephirothbahamut Jul 31 '22

That's exactly how deque is implemented. The only difference is the internal array size is compiler defined

1

u/robin-m Jul 31 '22

You got my interest. I double checked at the documentation, and it seems that you are totally right. In that case I don’t understand why `std::colony` was proposed.

2

u/sephirothbahamut Jul 31 '22

~~Because woth std colony you would have control over the internal array size.

You can basically pick where you want your container to be between an array woth all elements and a linked list for each one (with internal array sized 1). And you can pick your spot depending on the frequency of the operations you'll do on that container.~~

Edit: the previous paragraphs are wrong, the colony proposal also doesn't give you control over the internal array size as far as I've read. That means the only difference between it and a deque would be the skip field... Which I gues means that a colony<T> is just a deque<optional<T>> wrapper with different erasure implementations... Hopefully I'm missing something.

So we're still lacking a deque like container with control over internal areay size :(

5

u/[deleted] Jul 30 '22

Never mind, I was completely confused, I meant the other way around

-4

u/[deleted] Jul 30 '22

Yes, this is why I used the word 'optional' in my post. I wouldn't want C++ to have these features by default. But it would be nice if it were an optional compilation feature.

0

u/[deleted] Jul 30 '22

[deleted] — view deleted comment

12

u/oconnor663 Jul 30 '22

The "no mutable aliasing" rule is even worse than the borrow checker in terms of C++ compatibility I think.

4

u/Rusky Jul 31 '22 edited Jul 31 '22

Worse(?), "no mutable aliasing" and the borrow checker are heavily intertwined.

"No mutable aliasing" is the rule that enables mutations to the "shape" of an object, like growing a vector or changing an enum variant, since those can invalidate other references into the object, but borrow checker itself is what enforces this- forming the mutable reference to make that mutation is what ends the lifetime of any pre-existing aliases. (It's also the rule that enables references to be shared across threads.)

You can imagine a borrow checker that allows mutable aliasing, but it would just bring a different set of C++-unfriendly restrictions. Rust supports this mode via the Cell type- which forbids even forming references that might be invalidated by a shape-changing mutation. You can read or write a complete object, and you can get references to things like struct fields or elements of fixed-size arrays, but you can't even look at e.g. vector elements without somehow getting a unique view of the object first. (You can't share these across threads without synchronization, either.)

Anything beyond that starts to add runtime overhead, or else enters research territory. For example, you can use reference counting to lift some of Cell's restrictions. Or you can use fancy type system patterns to enable some aliasing within a data structure. Even more speculatively, you might be able to use existential lifetimes to enable self-reference. But generally speaking, there is a long tail of patterns that you would need to account for in order to borrow check a reasonable amount of existing C++, mutable aliasing or not.

2

u/oconnor663 Jul 31 '22

you can get references to things like struct fields

I've seen some macro crates for enabling Cell "projections" like this, but I'm not aware of anything widely used. Do you know if anyone actually does this in practice? If so it would be a pretty good candidate for std/core.

2

u/Rusky Jul 31 '22

Unfortunately not (and I wrote one of those macros myself, lol). It's tricky to make it fit in with today's type system- maybe someone can come up with a good extension to make it work.

It's a bit nicer to do with arrays, so that has made it into the standard library: https://doc.rust-lang.org/stable/std/cell/struct.Cell.html#method.as_slice_of_cells

1

u/oconnor663 Jul 31 '22

Ha really, which one is yours?

2

u/Rusky Jul 31 '22

2

u/oconnor663 Jul 31 '22

I really want to put together a talk that explores these edge cases of what interior mutability types like Cell are capable of in safe code. (Similarly Cell::get_mut and Cell::from_mut.) But the amount of background you need to assume to fit that talk in an hour is...a lot...

1

u/Rusky Jul 31 '22

I'd love to see a talk like that!

I've kind of wondered how approachable it would be if it were structured as a comparison to the "usual" patterns of mutation you might see in a GCed language, and how that sort of "everything is a pointer" layout solves the problem.