Storing Weak Pointers in Containers

Can I store weak pointers in containers like std::vector or std::map?

Yes, you can store weak pointers in containers just like any other object type. Weak pointers can be stored in various standard containers such as std::vector, std::list, std::map, etc.

When storing weak pointers in containers, keep in mind that the container itself does not participate in the ownership or lifetime management of the pointed-to objects. The weak pointers in the container will not prevent the objects from being destroyed if all the corresponding shared pointers go out of scope.

Here's an example that demonstrates storing weak pointers in a std::vector:

#include <memory>
#include <vector>
#include <iostream>

int main() {
  std::vector<std::weak_ptr<int>> WeakPtrVector;

  {
    auto SharedPtr1{std::make_shared<int>(10)};
    auto SharedPtr2{std::make_shared<int>(20)};

    WeakPtrVector.emplace_back(SharedPtr1);
    WeakPtrVector.emplace_back(SharedPtr2);

    for (const auto& WeakPtr : WeakPtrVector) {
      if (auto SharedPtr{WeakPtr.lock()}) {
        std::cout << *SharedPtr << " ";
      }
    }
    std::cout << "\n";
  }

  for (const auto& WeakPtr : WeakPtrVector) {
    if (auto SharedPtr{WeakPtr.lock()}) {
      std::cout << *SharedPtr << " ";
    } else {
      std::cout << "Expired ";
    }
  }
}
10 20
Expired Expired

In this example, we create a std::vector called WeakPtrVector to store weak pointers to int objects. We create two shared pointers, SharedPtr1 and SharedPtr2, and add their corresponding weak pointers to the vector using emplace_back().

Inside the first block, we iterate over the WeakPtrVector and lock each weak pointer to obtain a shared pointer. If the object still exists, we dereference the shared pointer and print its value.

After the block ends, the shared pointers SharedPtr1 and SharedPtr2 go out of scope, and the pointed-to objects are destroyed. When we iterate over the WeakPtrVector again, locking the weak pointers will return expired shared pointers, indicating that the objects no longer exist.

Storing weak pointers in containers is useful in scenarios where you need to maintain a non-owning reference to objects without affecting their lifetimes. For example, you can use weak pointers in a cache or a lookup table where the actual ownership is managed elsewhere.

Just remember to always check the validity of the weak pointers using lock() or expired() before accessing the pointed-to objects, as the objects may have been destroyed since the weak pointers were added to the container.

Weak Pointers with std::weak_ptr

A full guide to weak pointers, using std::weak_ptr. Learn what they're for, and how we can use them with practical examples

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Detecting Expired Weak Pointers
How can I check if a weak pointer has expired before using it?
Creating a Shared Pointer from a Weak Pointer
How do I create a shared pointer from a weak pointer to access the pointed-to object?
Performance Impact of Weak Pointers
Do weak pointers have any performance overhead compared to raw pointers?
Using Weak Pointers in Multithreaded Environments
Are weak pointers thread-safe? How can I use them safely in multithreaded code?
Using Custom Deleters with Weak Pointers
Can I use custom deleters with weak pointers like I can with unique_ptr and shared_ptr?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant