How to enable_shared_from_this of both parent and derived

The OP solution can be made more convenient by defining the following on the base class. protected: template <typename Derived> std::shared_ptr<Derived> shared_from_base() { return std::static_pointer_cast<Derived>(shared_from_this()); } This can be made more convenient by placing it in a base class (for reuse). #include <memory> template <class Base> class enable_shared_from_base : public std::enable_shared_from_this<Base> { protected: template <class … Read more

How do I pass Rc to a function accepting Rc?

(An older revision of this answer essentially advised to clone the underlying struct and put it in a new Rc<RefCell<Box<MyTrait>> object; this was necessary at the time on stable Rust, but since not long after that time, Rc<RefCell<MyStruct>> will coerce to Rc<RefCell<MyTrait>> without trouble.) Drop the Box<> wrapping, and you can coerce Rc<RefCell<MyStruct>> to Rc<RefCell<MyTrait>> … Read more

How to check memory allocation failures with new operator?

Well, you call new that throws bad_alloc, so you must catch it: try { scoped_array<char> buf(new char[MAX_BUF]); … } catch(std::bad_alloc&) { … } or scoped_array<char> buf(new(nothrow) char[MAX_BUF]); if(!buf) { //allocation failed } What I mean by my answer is that smart pointers propagate exceptions. So if you’re allocating memory with ordinary throwing new, you must … Read more

Example to use shared_ptr?

Using a vector of shared_ptr removes the possibility of leaking memory because you forgot to walk the vector and call delete on each element. Let’s walk through a slightly modified version of the example line-by-line. typedef boost::shared_ptr<gate> gate_ptr; Create an alias for the shared pointer type. This avoids the ugliness in the C++ language that … Read more

Why would I std::move an std::shared_ptr?

I think that the one thing the other answers did not emphasize enough is the point of speed. std::shared_ptr reference count is atomic. increasing or decreasing the reference count requires atomic increment or decrement. This is hundred times slower than non-atomic increment/decrement, not to mention that if we increment and decrement the same counter we … Read more

Is auto_ptr deprecated?

UPDATE: This answer was written in 2010 and as anticipated std::auto_ptr has been deprecated. The advice is entirely valid. In C++0x std::auto_ptr will be deprecated in favor of std::unique_ptr. The choice of smart pointer will depend on your use case and your requirements, with std::unique_ptr with move semantics for single ownership that can be used … Read more

Why is auto_ptr being deprecated?

The direct replacement for auto_ptr (or the closest thing to one anyway) is unique_ptr. As far as the “problem” goes, it’s pretty simple: auto_ptr transfers ownership when it’s assigned. unique_ptr also transfers ownership, but thanks to codification of move semantics and the magic of rvalue references, it can do so considerably more naturally. It also … Read more