Please take into account my inexperience, but I do not understand the point of std::owner_less
.
I have been shown that a map
with weak_ptr
as key is not recommended because an expired weak_ptr
key will break the map, actually:
If it expires, then the container's order is broken, and trying to use the container afterwards will give undefined behaviour.
How undefined is that behavior? The reason I ask is because the docs say about owner_less
:
This function object provides owner-based (as opposed to value-based) mixed-type ordering of both std::weak_ptr and std::shared_ptr. The order is such that two smart pointers compare equivalent only if they are both empty or if they both manage the same object, even if the values of the raw pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)
Again, this is my inexperience talking, but it doesn't sound like the map
will be completely broken by an expired weak_ptr
:
Returns whether the weak_ptr object is either empty or there are no more shared_ptr in the owner group it belongs to.
Expired pointers act as empty weak_ptr objects when locked, and thus can no longer be used to restore an owning shared_ptr.
It sounds like it could become more flabby than completely undefined. If one's implementation removes expired weak_ptrs and simply doesn't or has no use for any lingering ones, when does the behavior become undefined?
If one's implementation has no regard for order, yet only needs a convenient way to associate weak_ptr
s with data, is the behavior still undefined? In other words, will find
start to return the wrong key?
Map
The only problem that I can find in the docs is what's referenced above, that expired weak_ptrs will return equivalent.
According to these docs, this isn't a problem for implementations that do not rely on ordering nor have use for expired weak_ptr
s:
Associative
Elements in associative containers are referenced by their key and not by their absolute position in the container.
Ordered
The elements in the container follow a strict order at all times. All inserted elements are given a position in this order.
Map
Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value.
That sounds like if an implementation is not concerned with order nor has use for expired weak_ptr
s then there is no problem because values are referenced by key not by order, so find
ing an expired weak_ptr
will return possibly another weak_ptr
s value, but since there's no use for it in this particular implementation except to be erase
d, there's no problem.
I can see how a need to use weak_ptr
ordering or expired weak_ptr
s could be a problem, whatever application that may be, but all behavior seems far from undefined, so a map
or set
does not seem to be totally broken by an expired weak_ptr
.
Are there more technical explanations of map
, weak_ptr
, and owner_less
that refute these docs and my interpretation?