Why is shared_ptr legal, while unique_ptr is ill-formed?

It is because std::shared_ptr implements type-erasure, while std::unique_ptr does not. Since std::shared_ptr implements type-erasure, it also supports another interesting property, viz. it does not need the type of the deleter as template type argument to the class template. Look at their declarations: template<class T,class Deleter = std::default_delete<T> > class unique_ptr; which has Deleter as type … Read more

How to assign the address of an existing object to a smart pointer?

Try std::unique_ptr::reset void foo(bar &object){ std::unique_ptr<bar> pointer; pointer.reset(&object); } But be aware this is not recommended, you should not create a unique_ptr to a reference that is being passed to a function. At the end of the function, when pointer is being destroyed it will try to destroy object as well, and it won’t be … Read more

What is the best smart pointer return type for a factory function?

I’ll answer in reverse order so to begin with the simple cases. Utility functions that accept objects from the factory functions, use them, but do not take ownership. (For example a function that counts the number of words in the document.) If you are calling a factory function, you are always taking ownership of the … Read more

How to perform a dynamic_cast with a unique_ptr?

Update The question has been clarified: sorry I was not clear. I want the ownership to remain with original owner, the called function should only get reference to it, not ownership. Not looking for two smart pointer for the same object. In that case, the solution is simply: dynamic_cast<B&>(*my_unique_ptr) Done. It throws if the cast … Read more

Passing unique_ptr to functions

In Modern C++ style, there are two keys concepts: Ownership Nullity Ownership is about the owner of some object/resource (in this case, an instance of Device). The various std::unique_ptr, boost::scoped_ptr or std::shared_ptr are about ownership. Nullity is much more simple however: it just expresses whether or not a given object might be null, and does … Read more

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

Bad practice to return unique_ptr for raw pointer like ownership semantics?

Returning a std::unique_ptr from a factory method is just fine and should be a recommended practice. The message it conveys is (IMO): You are now the sole owner of this object. Furthermore, for your convenience, the object knows how to destroy itself. I think this is much better then returning a raw pointer (where the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)