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

std::shared_ptr thread safety explained

As others have pointed out, you’ve got it figured out correctly regarding your original 3 questions. But the ending part of your edit Calling reset() in thread IV will delete previous instance of A class created in first thread and replace it with new instance? Moreover after calling reset() in IV thread other threads will … Read more

Why are two raw pointers to the managed object needed in std::shared_ptr implementation?

The reason for this is that you can have a shared_ptr which points to something else than what it owns, and that is by design. This is implemented using the constructor listed as nr. 8 on cppreference: template< class Y > shared_ptr( const shared_ptr<Y>& r, T *ptr ); A shared_ptr created with this constructor shares … Read more

Conversion from boost::shared_ptr to std::shared_ptr?

Based on janm’s response at first I did this: template<class T> std::shared_ptr<T> to_std(const boost::shared_ptr<T> &p) { return std::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } template<class T> boost::shared_ptr<T> to_boost(const std::shared_ptr<T> &p) { return boost::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } But then I realized I could do this instead: namespace { template<class SharedPointer> struct Holder { … Read more

Alternatives of static_pointer_cast for unique_ptr

#Raw pointers The solution for your problem is to get the raw (non-owning) pointer and cast it – then just let the raw pointer go out of scope and let the remaining unique_ptr<Base> control the lifetime of the owned object. Like this: unique_ptr<Base> foo = fooFactory(); { Base* tempBase = foo.get(); Derived* tempDerived = static_cast<Derived*>(tempBase); … Read more

Does using .reset() on a std::shared_ptr delete all instances

When you use .reset(), you are eliminating one owner of the pointer, but all of the other owners are still around. Here is an example: #include <memory> #include <cstdio> class Test { public: ~Test() { std::puts(“Test destroyed.”); } }; int main() { std::shared_ptr<Test> p = std::make_shared<Test>(); std::shared_ptr<Test> q = p; std::puts(“p.reset()…”); p.reset(); std::puts(“q.reset()…”); q.reset(); std::puts(“done”); … Read more

How to intentionally delete a boost::shared_ptr?

You just do ptr.reset(); See the shared_ptr manual. It is equivalent to shared_ptr<T>().swap(ptr) You call reset on every smart pointer that should not reference the object anymore. The last such reset (or any other action that causes the reference count drop to zero, actually) will cause the object to be free’ed using the deleter automatically. … Read more

Problems using member function as custom deleter with std::shared_ptr

std::shared_ptr<SDL_Surface>(SDL_LoadBMP(….), [=](SDL_Surface* surface) { std::cout << “Deleting surface\n”; SDL_FreeSurface(surface); }); or void DeleteSurface(SDL_Surface* surface) { std::cout << “Deleting surface\n”; SDL_FreeSurface(surface); } std::shared_ptr<SDL_Surface>(SDL_LoadBMP(….), DeleteSurface); EDIT: Seeing your updated question, DeleteSurface should be a non-member function, otherwise you need to use std::bind or std::mem_fn or some other member function pointer adapter.

boost, shared ptr Vs weak ptr? Which to use when?

In general and summary, Strong pointers guarantee their own validity. Use them, for example, when: You own the object being pointed at; you create it and destroy it You do not have defined behavior if the object doesn’t exist You need to enforce that the object exists. Weak pointers guarantee knowing their own validity. Use … Read more

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