boostorg / boostorg/smart_ptr

shared_ptr operator < compares managed pointers instead of stored pointers

Open
#91 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
111
Forks
116
PR merge metrics
No merged PRs in 30d

Description

operator < of boost::shared_ptr compares the managed pointers:
```
template inline bool operator<(shared_ptr const & a, shared_ptr const & b) BOOST_SP_NOEXCEPT
{
return a.owner_before( b );
}
template bool owner_before( shared_ptr const & rhs ) const BOOST_SP_NOEXCEPT
{
return pn < rhs.pn;
}
```
This is also documented as
> under the equivalence relation defined by operator<, !(a < b) && !(b < a), two shared_ptr instances are equivalent if and only if they share ownership or are both empty.

However, std::shared_ptr compares the stored pointers. Comparing the managed pointers defeats the purpose of the aliasing constructor and is asymmetrical to operator==, which _does_ compare the stored pointers. See https://en.cppreference.com/w/cpp/memory/shared_ptr/operator_cmp:
> In all cases, it is the stored pointer (the one returned by [get()](https://en.cppreference.com/w/cpp/memory/shared_ptr/get)) that is compared, rather than the managed pointer (the one passed to the deleter when [use_count](https://en.cppreference.com/w/cpp/memory/shared_ptr/use_count) goes to zero). The two pointers may differ in a [shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr) created using the aliasing constructor.

See a minimal repro here: https://godbolt.org/z/xon8cxnss

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.