Weak Pointers with std::weak_ptr

Storing Weak Pointers in Containers

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

Illustration representing computer hardware

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.

This Question is from the Lesson:

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

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

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved