BDE 4.14.0 Production release
|
Provide an ownership hash functor for shared and weak pointers.
shared_ptr
and weak_ptr
Canonical header: bsl_memory.h
This component provides the C++26 standard functor, bsl::owner_hash
, that calculates the hash of a smart pointer object by the address of their bslma::SharedPtrRep
data. Note that this class is an empty POD type.
This section illustrates intended use of this component.
Suppose we need an unordered map accepting shared pointers as keys. We also expect that this container will be accessible from multiple threads and some of them will store weak versions of smart pointers to break reference cycles.
First, we define an owner-based equality predicate, that is required by the bsl::unordered_map
along with this owner-based hash.
Note that this struct is defined only to avoid cycle dependencies between BDE components. In real code for these purposes it is recommended to use bsl::owner_equal
.
Then, we create a container and populate it:
Now, we make sure, that shared pointers can be used to perform lookup:
Finally, we simulate the situation of accessing the container from another thread and perform lookup using weak pointers: