So, all of this taken together means that it is safe to use std::weak_ptr as they key of a std::map or in a std::set, as long as you use std::owner_less to perform the ordering. Otherwise, the weak_ptr would have a dangling pointer reference. When the shared_ptr is destroyed, it may destroy the data, but the reference count object has to remain until all of the weak_ptr that share ownership are removed. When a weak_ptr is constructed from that shared_ptr, it will point to that same control structure and reference count. An expired weak_ptr simply has a use_count of zero.Īs a practical matter, when a shared_ptr is created, a reference count object has to be created, either separate from the data using the shared_ptr constructor, or in the same memory allocation when std::make_shared is used. It's also important to note that you cannot get an empty weak_ptr by simply letting the weak_ptr expire. They key to note here is that the only way to create an empty weak_ptr is to default construct it, or copy/move/assign one from a previously empty weak_ptr or shared_ptr. Swap is defined as swapping the states of the two weak_ptrs, and assignment is defined as using the constructors above along with a swap. Postconditions: use_count() = r.use_count(). Ownership with r and stores a copy of the pointer stored in r. Weak_ptr object otherwise, constructs a weak_ptr object that shares Requires: The second and third constructors shall not participate in the overload resolution unless Y* is implicitly convertible to T*.Įffects: If r is empty, constructs an empty Template weak_ptr(const shared_ptr& r) noexcept template weak_ptr(const weak_ptr& r) noexcept.Again, the standard statesĮffects: Constructs an empty weak_ptr object. The key is that "empty" in the standard means that the weak_ptr does not share ownership with any object. ![]() Now, I believe the confusion is over the second term.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |