Skip to content

Commit c8acb1f

Browse files
committed
Clarify README
1 parent da73d98 commit c8acb1f

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The only difference between `observable_unique_ptr` and `observable_sealed_ptr`
2929

3030
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.
3131

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`.
3333

3434

3535
## Usage
@@ -52,27 +52,29 @@ int main() {
5252
oup::observer_ptr<std::string> obs_ptr;
5353

5454
{
55-
// Unique pointer that owns the object
55+
// Sealed (unique) pointer that owns the object
5656
auto owner_ptr = oup::make_observable_sealed<std::string>("hello");
5757

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+
5862
// Make the observer pointer point to the object
5963
obs_ptr = owner_ptr;
6064

61-
// Observer pointer is valid
65+
// The observer pointer is now valid
6266
assert(!obs_ptr.expired());
6367

6468
// It can be used like a regular raw pointer
6569
assert(obs_ptr != nullptr);
6670
std::cout << *obs_ptr << std::endl;
6771

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
7375
}
7476

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,
7678
// the observer pointer is now null.
7779
assert(obs_ptr.expired());
7880
assert(obs_ptr == nullptr);

0 commit comments

Comments
 (0)