I would like to compare boost::intrusive_ptr instances with different const-ness of underlying type, but unfortunately it doesn't work from the box if you uncomment the line:
#include <boost/intrusive_ptr.hpp>
#include <memory>
struct S {};
void intrusive_ptr_release(const S*){}
int main() {
boost::intrusive_ptr<const S> boost_const_ptr;
boost::intrusive_ptr<S> boost_ptr;
// if(boost_const_ptr < boost_ptr) {} // <------ here
const S* s_const_ptr;
S* s_ptr;
if(s_const_ptr < s_ptr) {}
std::unique_ptr<const S> u_const_ptr;
std::unique_ptr<S> u_ptr;
if(u_const_ptr < u_ptr) {}
}
The error will be:
no match for 'operator<' (operand types are 'boost::intrusive_ptr<const S>' and 'boost::intrusive_ptr<S>')
I can compare raw pointers, or even std::unique_ptrs, but not boost::intrusive_ptr. Is there a reason for it, or just an oversight?
A possible reason is that equivalent semantics to
shared_ptr< would be impossible in general. Other smart pointer types don't have to deal with the possibility of heterogenous comparison1, butshared_ptr,weak_ptrandintrusive_ptrdo.shared_ptris able to do that by comparing addresses of the ref count.intrusive_ptrcan't do that, because it doesn't have pointer access to the ref count.shared_ptr<Base>to ashared_ptr<Derived>they might share ownership of the same object, which is impossible forunique_ptr<Base>andunique_ptr<Derived>.