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
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 <class T2>
friend std::size_t hash_value(custom_type<T2> x);
std::size_t hash(custom_type x)
{
__boost_hash<T> hasher;
return hasher(value);
}
};
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 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 <class T>
std::size_t hash_value(foo::custom_type<T> x)
{
boost::hash<T> hasher;
return hasher(x.value);
return x.hash();
}
}

View File

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