Boost logo

Boost :

From: Daniel James (daniel_at_[hidden])
Date: 2005-03-12 04:08:30


JOAQUIN LOPEZ MU?Z wrote:
> De: Alberto Barbati <abarbati_at_[hidden]>
>>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.

Ah...

And even if it works, it isn't very good when sizeof(T) is greater than
the alignment of T.

>>IMHO, the only reasonable way to map generic pointer values to
>>integer
>>values is through reinterpret_cast<>. The mapping would be
>>implementation-defined, but it would be no worse than the intent
>>of the
>>proposed implementation (if it worked).
>
>
> Agreed.

This hash value will have problems with alignment. Although, that won't
matter with our containers because we use a prime number of buckets
(unless your platform has 53-byte alignment...). But if this hash
function is to be general purpose it's worth thinking about.

> Please, no SFINAE! This would break not-so-conforming
> compilers. Let's stick to compilerwise macro-based branches.
> After all, for the vast majority of platforms (AFAIK)
> sizeof(size_t)==sizeof(T*)

It would be possible to implement Alberto's suggestion without SFINAE.

Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk