I would like to define a shared pointer whose operator==
compares the pointed to values instead of the pointers, something like this:
template <typename T>
struct deref_shared_ptr: std::shared_ptr<T> {
bool operator==(const deref_shared_ptr rhs) const {
return (**this == *rhs);
}
};
This type has two potential problems:
It derives from a standard library type.
It violates the Liskov substitution principle, since it changes the semantics of
operator==
.
However, I am thinking that neither of the two is a problem unless one uses the base type pointer/reference to refer to the derived type. In particular, these problems might never occur in template code. For shared pointers the situation is even better: shared pointers are stored and passed by value.
Is my logic correct? Are there still good reasons to avoid the above definition in favor of composition?