Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared_ptr operator < compares managed pointers instead of stored pointers #91

Open
visrue opened this issue Apr 13, 2022 · 1 comment

Comments

@visrue
Copy link

visrue commented Apr 13, 2022

operator < of boost::shared_ptr compares the managed pointers:

template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
{
    return a.owner_before( b );
}
template<class Y> bool owner_before( shared_ptr<Y> 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()) that is compared, rather than the managed pointer (the one passed to the deleter when use_count goes to zero). The two pointers may differ in a shared_ptr created using the aliasing constructor.

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

visrue added a commit to visrue/boost_smart_ptr that referenced this issue Apr 13, 2022
@pdimov
Copy link
Member

pdimov commented May 19, 2022

The current semantics of operator< are intentional. They predate std::shared_ptr. The committee changed operator< for shared_ptr to compare the pointer values, but that was after shared_ptr was added to the standard.

(std::shared_ptr also defines the rest of the comparison operators, and we do not.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants