Re:Emulating the Rust borrow checker with C++ move-only types
by Animats on Saturday May 13, 2017 @06:35PM
(#14333354)
Right. This isn't about borrowing; it's about taking ownership. Single ownership is half the problem. Enforcing borrow semantics means passing references/pointers you can use, but can't keep or delete. Trying to do this in C++ means either extreme restrictions on not being able to copy a reference/pointer, or the possibility of some function keeping a reference/pointer beyond the lifetime of the object. There's no way to say "it's OK to pass or copy this to a scope with a shorter lifetime". The C++ type system doesn't understand "inner scope".
Trying to retrofit memory safety to C++ without breaking backwards compatibility is hard. I struggled with this years ago. It turns out it's in some ways easier to do for C than for C++.[1]
Trying to retrofit memory safety to C++ without breaking backwards compatibility is hard. I struggled with this years ago. It turns out it's in some ways easier to do for C than for C++.[1]
[1] http://www.animats.com/papers/languages/safearraysforc27.pdf