c++,key,unordered-map,weak-ptr,websocket++
boost::unordered_map could be slow on iteration, thus, I would suggest that you could store all websocketpp::connection_hdl in a std::vector. For the map you can use pointers as keys: boost::unordered_map<websocketpp::connection_hdl*, X>
c++,c++11,shared-ptr,smart-pointers,weak-ptr
Approach this from the conceptual side, not the implementation one. Smart pointers represent ownership. And existence of smart pointers does not invalidate the role of raw pointers as non-owning observers. Does each object have a single, clearly defined owner (e.g. a graph owns all of its vertices and edges)? If...
c++,map,initialization,find,weak-ptr
It's safe to call find (and count), as long as whatever ordering you've defined doesn't rely on the pointer being non-empty. The only thing that find (and count) will do with the argument is use it as an argument for the comparator. However, it's not safe to use weak_ptr as...
c++,c++11,stl,shared-ptr,weak-ptr
The shared_ptr constructor you're trying to use is explicit. template< class Y > explicit shared_ptr( const std::weak_ptr<Y>& r ); Your first example std::shared_ptr<sfg::Notebook> strongNotebook(weakNotebook); works because you're using direct initialization, which works with explicit constructors. You'd have the same problem if you used copy initialization instead std::shared_ptr<sfg::Notebook> strongNotebook = weakNotebook;...
A possible use of this "design" could be to use m_self.lock() to generate shared pointers from this. If you remove this weak pointer member, the reference count hold by the generated shared pointer from this would be incorrect. It achieves the same than std::enable_shared_from_this, interestingly enough, cppreference.com mentions this design...
c++,c++11,shared-ptr,smart-pointers,weak-ptr
No. There is no interface for doing this thing, because it would miss the entire point. The resource is released if no std::shared_ptr refers to it. By using std::weak_ptr you specifically allow your resource to be released. Don't use it if that's not what you want....
Calling weak_ptr::lock() multiple times is ok, the returned shared_ptr is configured properly, i.e. all shared_ptr-s having the common pointee are sharing the reference counter. Note that lock() returns non-null pointer iff there is at least one shared_ptr pointing to the object. If there was none then the object would have...
c++,pointers,c++11,smart-pointers,weak-ptr
If we have a Parent class that has a reference to a Child Class (shared pointer) and the Child Class also has a reference to the Parent Class (again with a shared pointer) we have to make one of these two smart pointers a weak pointer. No, you don't...
One of the reasons std::owner_less exists is to provide this ordering, and guarantee its safety in the presence of expiring weak pointers. My logic is First, the definition of std::owner_less operator() defines a strict weak ordering as defined in 25.4 under the equivalence relation defined by operator(), !operator()(a, b) &&...
c++,shared-ptr,smart-pointers,unique-ptr,weak-ptr
std::weak_ptr can't be used unless you're converting in to std::shared_ptr by the means of lock(). if C++ did what you suggested, that means that you need to convert std::weak_ptr to unique in order to use it, violating the uniqueness (or re-inventing std::shared_ptr) In order to illustrate, look at the two...
ios,objective-c,weak-references,weak-ptr
That check is unnecessary, and is giving you a false sense of security. Here's the problem: __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ if (!weakSelf) { return; } // THE LINE OF INTEREST [weakSelf doSomething]; }); At THE LINE OF INTEREST, some other thread might clear the last strong reference...
c++,c++11,lambda,shared-ptr,weak-ptr
Extend object lifetime Lambdas can capture shared pointers to this, so an object won't die while at least one lambda exists. class Foo : public std::enable_shared_from_this<Foo> { public: Foo(const std::string& i_name) : name(i_name) {} std::function<void()> GetPrinter() { std::shared_ptr<Foo> that = shared_from_this(); return [that]() { std::cout << that->name << std::endl; };...
c++,c++11,destructor,shared-ptr,weak-ptr
Bizarrely, you dynamically allocated the shared_ptr, and never did anything to destroy it. If the shared_ptr isn't destroyed, neither will be the thing it points to. It's really not clear what you're trying to do here. You're writing strange, awkward code with no use case and wondering how to "make...
c++,c++11,shared-ptr,unique-ptr,weak-ptr
You can do this with pure Standard C++ using shared_ptr<unique_ptr<T>>. Observers received only a shared_ptr<const unique_ptr<T>>, allowing them to look but not touch. The owner, having a non-const smart pointer, can at any time call reset() on the unique_ptr to destroy the instance. At that time all the observers can...