3 ms·
C++ also has a fair number of built in STL smart-pointer types that do approximately the same thing as the "move/borrow" semantics which everyone gets excited a
by derefr 8y ago
C++ also has a fair number of built in STL smart-pointer types that do approximately the same thing as the "move/borrow" semantics which everyone gets excited about in Rust.
These alone are a worthwhile "dialect" on top of C, the use of which results in a far safer C; and—as far as I know—these smart-pointer types and their compile-time semantics would be impossible to implement without C++'s additional layered-on "C with classes" type system.
(It's clear that these smart-pointer types are a valuable addition to C, because they were independently implemented as part of all sorts of other C macro libraries in the early 90s. I think there were at least four C smart-pointer implementations that existed as part of various parts of Windows, for example.)
- sifoobar 8y agoSTL smart pointers are all fun and games until you start using threads, at which point you realize that automagically flipping the (now atomic) counter back and forth behind the scenes every time the pointer can't be safely passed as a reference is a performance disaster. Manually referencing and de-referencing exactly when needed avoids most of this. Sometimes it's perfectly safe to pass the same reference through several layers without changing the number of references. As an added bonus YOU get do decide which references, if any, need to support multiple threads.
- gpderetta 8y agoReference counted smart pointers are just one of the many smart pointer types. Atomically reference counted smart pointers are a smaller subset of those.
- sifoobar 8y agoRight, if the discussion isn't going your way you can always side track by making up faults and projecting agendas. How is this helping us go anywhere worth going? I was talking about std::shared_ptr, which is the closest thing to reference counting that STL offers.
- derefr 8y agoI think the GP was accusing you of "side tracking" by bringing up std::shared_ptr in the first place. My post above was about how C++ smart-pointers compare favorably to Rust's oft-considered-"unique" ownership/borrowing system. The particular STL features that cause equivalent "compile-time magic" to happen to what you get from Rust's semantics, are those of std::uniq_ptr and std::move, not those of std::shared_ptr. So the value (or lack thereof) of std::shared_ptr, isn't really a knock against the value of the C++ STL smart-pointers collection as a whole—you can entirely ignore the existence of std::shared_ptr (I do!) and still think C++ STL smart-pointers are the cat's pyjamas.
- kllrnohj 8y agostd::move exists and works perfectly with std::shared_ptr. No need to do any atomic work at all when moving around std::shared_ptr's
- sifoobar 8y agoThank you, that would have helped. Still, it's just too many layers of complexity for my taste. Nailing performance problems in C is a Sunday walk in the park in comparison. Live and let live, I was just sharing my experience of going back and forth.