-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart-ptrs-weak-cpp11.cpp
More file actions
56 lines (40 loc) · 1.69 KB
/
smart-ptrs-weak-cpp11.cpp
File metadata and controls
56 lines (40 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
namespace {
//// weak_ptr — non-owning observer | Rust: Weak<T>
// - observes a shared_ptr without affecting ref count
// - must lock() to access — safely handles object being destroyed
//
// Key notes:
// - weak_ptr never keeps object alive — purely observing
// - always check lock() result before use — never assume it's valid
// - Rust Weak<T>: same concept, upgrade() = lock(), also returns Option
// - main use cases: breaking cycles, caches, observer/event systems
TEST_CASE("weak-1") {
//// create
auto sp = std::make_shared<int>(42);
std::weak_ptr<int> wp = sp; // ref count stays 1 ✓
//// access — must lock first
//// !! lock is like a temporary shared_ptr to keep the obj alive
if (auto locked = wp.lock()) { // locked: shared_ptr — empty if expired
std::cout << *locked; // ✓ object still alive
} else {
std::cout << "expired"; // sp was destroyed
}
//// "safe" checks without locking
wp.expired(); // true if object gone
wp.use_count(); // current ref count (0 if expired)
//// reset
wp.reset(); // release observation
//// main use 1 — break cycles
struct Node {
shared_ptr<Node> next;
// shared_ptr<Node> prev; // ❌ cycle if two nodes point to each other
weak_ptr<Node> prev; // ✓ breaks cycle — no ref count bump
};
}
}