Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2005-03-11 20:10:52

Alberto Barbati <abarbati_at_[hidden]> writes:

> Hi,
> I have a doubt about the proposed implementation of hash_value() for
> pointers, which is:
> template <class T> inline std::size_t hash_value(T* v)
> {
> return static_cast<std::size_t>(v - (T*)0);
> }
> this code calls for undefined behaviour according to §5.7/6, each time v
> is a non-null pointer.

Furthermore, you can't subtract addresses that aren't in the same
array. 0 isn't in the same array as anything.

> Unfortunately, there's one more thing... even if p == q,
> reinterpret_cast<uintmax_t>(p) is not guaranteed to be equal to
> reinterpret_cast<uintmax_t>(q) on implementations that have multiple
> internal representations for the same address. I don't think much can be
> done (in a portable way) for such platforms.

Once you're into reinterpret_cast you've left the land of strict
portability anyway.

Dave Abrahams
Boost Consulting

Boost list run by bdawes at, gregod at, cpdaniel at, john at