From 1f657699a2ed01ba99d79121ba3d94b354e261a7 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 9 Feb 2006 19:24:04 +0000 Subject: [PATCH] Tweak the portable example. [SVN r32787] --- hash/doc/hash.qbk | 25 +++++++++++++++---------- hash/examples/portable.cpp | 10 ++++++---- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/hash/doc/hash.qbk b/hash/doc/hash.qbk index f88f5fe..47a551d 100644 --- a/hash/doc/hash.qbk +++ b/hash/doc/hash.qbk @@ -306,7 +306,7 @@ but on a compiler which doesn't support ADL `hash_value` won't be found. To make things worse, some compilers which do support ADL won't find a friend class defined inside the class. -So move the member function out of the class: +So first move the member function out of the class: namespace foo { @@ -317,20 +317,26 @@ So move the member function out of the class: public: custom_type(T x) : value(x) {} - template - friend std::size_t hash_value(custom_type x); + std::size_t hash(custom_type x) + { + __boost_hash hasher; + return hasher(value); + } }; template - friend inline std::size_t hash_value(custom_type x) + inline std::size_t hash_value(custom_type x) { - __boost_hash hasher; - return hasher(x.value); + return x.hash(); } } -Now compilers which can't find the friend inside the class will find it. -On compilers which don't support ADL, define in the boost namespace: +Unfortunately, I couldn't declare hash_value as a friend, as some compilers +don't support template friends, so instead I declared a member function to +calculate the hash, can called it from hash_value. + +For compilers which don't support ADL, hash_value needs to be defined in the +boost namespace: #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP namespace boost @@ -341,8 +347,7 @@ On compilers which don't support ADL, define in the boost namespace: template std::size_t hash_value(foo::custom_type x) { - boost::hash hasher; - return hasher(x.value); + return x.hash(); } } diff --git a/hash/examples/portable.cpp b/hash/examples/portable.cpp index 8919c8c..c03b8a9 100644 --- a/hash/examples/portable.cpp +++ b/hash/examples/portable.cpp @@ -19,8 +19,11 @@ namespace foo public: custom_type(T x) : value(x) {} - template - friend std::size_t hash_value(foo::custom_type x); + std::size_t hash() const + { + boost::hash hasher; + return hasher(value); + } }; } @@ -33,8 +36,7 @@ namespace foo template std::size_t hash_value(foo::custom_type x) { - boost::hash hasher; - return hasher(x.value); + return x.hash(); } }