From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-03-16 12:59:44
Dave Harris wrote:
> In-Reply-To: <019d01c52985$a04f54f0$6801a8c0_at_pdimov>
> pdimov_at_[hidden] (Peter Dimov) wrote (abridged):
>> size_t Derived::hash_value() const
>> size_t seed = my_unique_value;
>> hash_combine( seed, Base::hash_value() );
>> hash_combine( seed, m_value );
>> return seed;
> That leaves the problem of assigning a different my_unique_value to
> each subclass, bearing in mind that different subclasses may be
> written by different people, teams or organisations. Without some
> kind of system
> people may just copy and paste code from one class to another and not
> change the value.
Perhaps, but this is the only correct way to write Derived::hash_value.
Consider the properly designed hierarchy of an abstract Base and two Derived
classes, both having an int member. Without using typeid().name() or an
unique seed, there will be collisions.
Again, we can hide these errors from the users by making some cases produce
reasonable hash values when the hash function is incorrectly written. I view
this as a disservice.