According to cppreference.com, std::shared_ptr
provides a full set of relative operators (==, !=, <, ...), but the semantics of comparison aren't specified. I assume they compare the underlying raw pointers to the referenced objects, and that std::weak_ptr and std::unique_ptr do the same.
For some purposes, I would prefer to have relative operators that order the smart pointers based on comparing the referenced objects (rather than the pointers to them). This is already something I do a lot, but with my own "dumb pointers" that behave mostly like raw pointers except for the relative operators. I'd like to do the same thing with the standard C++11 smart pointers too. So...
Is it OK to inherit from the C++11 smart pointers (shared_ptr, weak_ptr and unique_ptr) and override the relative operators?
Are there any sneaky issues I need to look out for? For example, are there any other methods I need to implement or use
using
for to ensure things work correctly?For the ultimate in laziness, is there a library template available that will do this for me automatically?
I'm hoping this is an "of course you can do that, idiot!" kind of thing, but I'm a little uncertain because there are some classes in the standard library (containers like std::map
at least) that you're not supposed to inherit from.
dereference_compare
that does what you want, keep the concepts separate. – GManNickGstd::set<std::shared_ptr<T>>
is different fromstd::set<std::shared_ptr<T>, indirect_less<std::shared_ptr<T>>>>
(whereindirect_less
is left to the imagination of the reader, but should be obvious). – Luc Danton