Unwanted implicit conversions to bool

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello everyone, i am having a bit of a problem using boost/hash in combination with boost/shared_ptr. Essentially, what i want when i hash a shared_ptr object is to hash the object it points to. So i wrote a template function to that effect, i.e. inline std::size_t hash_value(boost::shared_ptr<const IHashable> obj) { return obj->hash_value(); } where IHashable is an interface forcing the existence of the hash_value() function. Now i am getting compiler errors because apparently there is an implicit conversion to bool from boost::shared_ptr<T> and obviously there is a hash function for bool. Thus, something like boost::shared_ptr<IHashable> p(new something()); boost::hasher<something> hasher; std::size_t hash = hasher(p); is now ambiguous. I already found the BOOST_HASH_NO_IMPLICIT_CASTS switch, but it does not seem to change anything. I still get compiler errors like this one: In file included from /usr/include/boost/functional/hash/hash.hpp: 494:0, from /usr/include/boost/functional/hash.hpp:6, from types/../common.h:14, from types/variables.h:11, from types/abstractformula.h:11, from types/unaryoperations.h:11, from types/unaryoperations.cpp:1: types/unaryoperations.cpp:49:37: instantiated from here /usr/include/boost/functional/hash/extensions.hpp:176:34: error: call of overloaded ?hash_value(const boost::shared_ptrterms::CAbstractFormula&)? is ambiguous /usr/include/boost/functional/hash/hash.hpp:120:24: note: candidates are: size_t boost::hash_value(bool) types/../common.h:86:24: note: size_t terms::hash_value(boost::shared_ptr<const terms::IHashable>) Any idea on what i could to to prevent this would be greatly appreciated. cheers, Dominik -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQIcBAEBAgAGBQJNy70+AAoJEPKXvh6MABK35dkQAKRFv48yZ7XyZh/6F+SSh02G IBfSEMi/xMXerLRH+/FV2lB6/gx1tymkYXxF02gopfIlavHHTWPz0vnkqZYFAABq ZzLMpggx4G50KtUGZfeKKfMGdbR5+0AHHT5ZRuL6aTzsvgr6nMYDdxwBKlAPvTJv nDuH8fHaV2QU6tw8OSmqzsdtiKrA4rnSqLPg4T6g/e/EqvH2Pf8GkXowRVK0T9Sq uAPnqcNZcMPuaIbtACC+AzsVA3CBw5fFQktRVbkLUtqDRVr10a9qOxgnDo6b7Duz c/KGNnWH2n1Lnwp9GbrvZoScqwThr9WqBvdcITD3ivgAkWhy5Fy6pnR1leRZhbZ7 +eFv3c1tY7b4yE7I9qCfRwW8LDhnkAJ/nAdcmGhWNX8RRSkETTguoCe9KUPMPoSs MBUEVk8VUNC9J3/JQmxlDhVju4WvJjlAnYrf7zir2XTFQ7v5eP86BeXkYArl+eyg OI2qRVp2ZuhReorwDc09Hef4/NrXPy3Bpq5nEWsref/mBbsyeopH3wqTZBrtruky RtSiJInhm4CETfMVJ8aJh/QO3MzGw2PXRLLCQ/mR2xHRgvEWOH53r84/1s8H3uSA URvXvPLKBuTpog30+GhHgfWOYx1/1Hy3atHbR9XikNW9y6Pv8IxUISA4AaQXNZyn XkjTDMHQ5XAqj/6Etgr7 =ak4K -----END PGP SIGNATURE-----

On 12.05.2011 12:58, Dominik Steenken wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Hello everyone,
i am having a bit of a problem using boost/hash in combination with boost/shared_ptr. Essentially, what i want when i hash a shared_ptr object is to hash the object it points to. So i wrote a template function to that effect, i.e.
inline std::size_t hash_value(boost::shared_ptr<const IHashable> obj) { return obj->hash_value(); }
where IHashable is an interface forcing the existence of the hash_value() function. Now i am getting compiler errors because apparently there is an implicit conversion to bool from boost::shared_ptr<T> and obviously there is a hash function for bool. Thus, something like
boost::shared_ptr<IHashable> p(new something()); boost::hasher<something> hasher; std::size_t hash = hasher(p);
is now ambiguous.
Add
inline std::size_t hash_value(boost::shared_ptr<IHashable> obj)
{
return hash_value(static_cast

On 12 May 2011 11:58, Dominik Steenken
i am having a bit of a problem using boost/hash in combination with boost/shared_ptr. Essentially, what i want when i hash a shared_ptr object is to hash the object it points to.
That's incorrect because 'boost::hash' is intended to be used with 'operator=='. For shared_ptr, operator== compares the pointer, so boost::hash should hash the pointer, not the object it points to. If you want to do that, you need to create your own custom hash function, although you could use Boost.Hash to implement it.

Den 12-05-2011 12:58, Dominik Steenken skrev:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
inline std::size_t hash_value(boost::shared_ptr<const IHashable> obj) { return obj->hash_value(); }
Remark: always write foo( const boost::shared_ptr<T>& obj) to avoid needless update of the ref count/weak count. -Thorsten
participants (4)
-
Daniel James
-
Dominik Steenken
-
Sebastian Redl
-
Thorsten Ottosen