11 ms·
C++ introduced non-null references. But they didn't enforce them. There are still "I'm so l33t I can use null references" people. The new move semantics use n
by Animats 11y ago
C++ introduced non-null references. But they didn't enforce them. There are still "I'm so l33t I can use null references" people. The new move semantics use null references for things moved from. C++ is trying to do Rust-like borrow checking without a borrow checker. Errors result in de-referencing null and crashing.
Rust doesn't have null, but it has Option<T>, which is often syntactic sugar for null.
It's not that null is bad in itself. It's that having variables which might be null need to be distinguished from ones which can't be null.
- mcguire 11y ago"Rust doesn't have null, but it has Option<T>, which is often syntactic sugar for null." In a sense, yes. But you have to do something positive before you can pretend that nothing is something. And the call to unwrap can be annotated with an argument as for why it can never fail, or why it's unimportant if it panics, or even be outlawed by a style guide. "It's not that null is bad in itself. It's that having variables which might be null need to be distinguished from ones which can't be null." Amen, brother.
- nhaehnle 11y agoThe new move semantics use null references for things moved from. No. When you move the contents out of somewhere like an std::vector instance, that object is left in an undefined but valid state. You're free to continue working with that object (though the only meaningful thing you can usually do without undefined behaviour is destruction or the equivalent of clear). That's very different from null references.
- Animats 11y agoRight; you can't have an array of references, and it's "unique_ptr", not "unique_ref". But pointers do get set to null. If p1 is a unique_ptr, and you do p2 = std::move(p1); p1's pointer is set to null. Further uses of p1 will fail, or crash, or something. p1.get() returns the underlying pointer or null, apparently escaping the unique_ptr protection. It's far inferior to Rust's borrow checker.