3 ms·
>There will be not more than 1 reference to the object at any time. Untrue: auto x = std::make_unique<int>(5); auto& y = *x; // There are now two muta
by Kranar 4y ago
>There will be not more than 1 reference to the object at any time.
Untrue:
auto x = std::make_unique<int>(5);
auto& y = *x; // There are now two mutable references to the object represented by x.
>That's a zero-overhead GC that is enforced at compile time.
It's not zero overhead unfortunately. Because unique_ptr is not trivially movable or trivially destructible, there are numerous optimizations that every current C++ compiler does not perform, for example being able to pass a unique_ptr in a register.
unique_ptr is reasonable given C++'s limitations, but it doesn't come close to being as good as Rust's Box, which is genuinely zero overhead and also guarantees that there's only 1 mutable reference to the object at any given time.