Tweak the portable example.

[SVN r32787]
This commit is contained in:
Daniel James
2006-02-09 19:24:04 +00:00
parent 9d41ce0eda
commit 1f657699a2
2 changed files with 21 additions and 14 deletions

View File

@@ -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 To make things worse, some compilers which do support ADL won't find
a friend class defined inside the class. 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 namespace foo
{ {
@@ -317,20 +317,26 @@ So move the member function out of the class:
public: public:
custom_type(T x) : value(x) {} custom_type(T x) : value(x) {}
template <class T2> std::size_t hash(custom_type x)
friend std::size_t hash_value(custom_type<T2> x); {
__boost_hash<T> hasher;
return hasher(value);
}
}; };
template <class T> template <class T>
friend inline std::size_t hash_value(custom_type<T> x) inline std::size_t hash_value(custom_type<T> x)
{ {
__boost_hash<T> hasher; return x.hash();
return hasher(x.value);
} }
} }
Now compilers which can't find the friend inside the class will find it. Unfortunately, I couldn't declare hash_value as a friend, as some compilers
On compilers which don't support ADL, define in the boost namespace: 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 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
namespace boost namespace boost
@@ -341,8 +347,7 @@ On compilers which don't support ADL, define in the boost namespace:
template <class T> template <class T>
std::size_t hash_value(foo::custom_type<T> x) std::size_t hash_value(foo::custom_type<T> x)
{ {
boost::hash<T> hasher; return x.hash();
return hasher(x.value);
} }
} }

View File

@@ -19,8 +19,11 @@ namespace foo
public: public:
custom_type(T x) : value(x) {} custom_type(T x) : value(x) {}
template <class T2> std::size_t hash() const
friend std::size_t hash_value(foo::custom_type<T2> x); {
boost::hash<T> hasher;
return hasher(value);
}
}; };
} }
@@ -33,8 +36,7 @@ namespace foo
template <class T> template <class T>
std::size_t hash_value(foo::custom_type<T> x) std::size_t hash_value(foo::custom_type<T> x)
{ {
boost::hash<T> hasher; return x.hash();
return hasher(x.value);
} }
} }