You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ The only difference between `observable_unique_ptr` and `observable_sealed_ptr`
29
29
30
30
These pointers are useful for cases where the shared-ownership of `std::shared_ptr` is not desirable, e.g., when lifetime must be carefully controlled and not be allowed to extend, yet non-owning/weak/observer references to the object may exist after the object has been deleted.
31
31
32
-
Note: Because of the unique ownership model, observer pointers cannot extend the lifetime of the pointed object, hence this library provides less thread-safety compared to `std::shared_ptr`/`std::weak_ptr`. This is also true of `std::unique_ptr`, and is a fundamental limitation of unique ownership. If this is an issue, you will need either to add your own explicit locking logic, or use `std::shared_ptr`/`std::weak_ptr`.
32
+
Note: Because of the unique ownership model, observer pointers cannot extend the lifetime of the pointed object, hence this library provides less safety compared to `std::shared_ptr`/`std::weak_ptr`. This is also true of `std::unique_ptr`, and is a fundamental limitation of unique ownership. If this is an issue, simply use `std::shared_ptr`/`std::weak_ptr`.
33
33
34
34
35
35
## Usage
@@ -52,27 +52,29 @@ int main() {
52
52
oup::observer_ptr<std::string> obs_ptr;
53
53
54
54
{
55
-
// Unique pointer that owns the object
55
+
// Sealed (unique) pointer that owns the object
56
56
auto owner_ptr = oup::make_observable_sealed<std::string>("hello");
57
57
58
+
// A sealed pointer cannot be copied but it can be moved
59
+
// auto tmp_copied = owner_ptr; // error!
60
+
// auto tmp_moved = std::move(owner_ptr); // OK
61
+
58
62
// Make the observer pointer point to the object
59
63
obs_ptr = owner_ptr;
60
64
61
-
// Observer pointer is valid
65
+
// The observer pointer is now valid
62
66
assert(!obs_ptr.expired());
63
67
64
68
// It can be used like a regular raw pointer
65
69
assert(obs_ptr != nullptr);
66
70
std::cout << *obs_ptr << std::endl;
67
71
68
-
// The unique pointer cannot be copied
69
-
auto tmp_copied = owner_ptr; // error!
70
-
71
-
// ... but it can be moved
72
-
auto tmp_moved = std::move(owner_ptr); // OK
72
+
// An observer pointer can be copied and moved
73
+
// auto tmp_copied = obs_ptr; // OK
74
+
// auto tmp_moved = std::move(obs_ptr); // OK
73
75
}
74
76
75
-
// The unique pointer has gone out of scope, the object is deleted,
77
+
// The sealed pointer has gone out of scope, the object is deleted,
0 commit comments