2005-04-11 22:26:01 +00:00
|
|
|
|
2009-03-09 20:56:23 +00:00
|
|
|
// Copyright 2005-2009 Daniel James.
|
2007-08-24 01:05:36 +00:00
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
2005-04-11 22:26:01 +00:00
|
|
|
|
2016-05-30 15:19:11 +01:00
|
|
|
// Force use of assert.
|
|
|
|
|
#if defined(NDEBUG)
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-12-29 13:58:48 +00:00
|
|
|
#include <boost/container_hash/hash.hpp>
|
2005-04-11 22:26:01 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
// This example illustrates how to customise boost::hash portably, so that
|
|
|
|
|
// it'll work on both compilers that don't implement argument dependent lookup
|
|
|
|
|
// and compilers that implement strict two-phase template instantiation.
|
|
|
|
|
|
|
|
|
|
namespace foo
|
|
|
|
|
{
|
2006-02-08 19:03:09 +00:00
|
|
|
template <class T>
|
|
|
|
|
class custom_type
|
2005-04-11 22:26:01 +00:00
|
|
|
{
|
2006-02-08 19:03:09 +00:00
|
|
|
T value;
|
|
|
|
|
public:
|
|
|
|
|
custom_type(T x) : value(x) {}
|
2005-04-11 22:26:01 +00:00
|
|
|
|
2006-02-09 19:24:04 +00:00
|
|
|
std::size_t hash() const
|
|
|
|
|
{
|
|
|
|
|
boost::hash<T> hasher;
|
|
|
|
|
return hasher(value);
|
|
|
|
|
}
|
2005-04-11 22:26:01 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
|
|
|
|
|
namespace boost
|
2006-02-08 19:03:09 +00:00
|
|
|
#else
|
|
|
|
|
namespace foo
|
|
|
|
|
#endif
|
2005-04-11 22:26:01 +00:00
|
|
|
{
|
2006-02-08 19:03:09 +00:00
|
|
|
template <class T>
|
|
|
|
|
std::size_t hash_value(foo::custom_type<T> x)
|
2005-04-11 22:26:01 +00:00
|
|
|
{
|
2006-02-09 19:24:04 +00:00
|
|
|
return x.hash();
|
2005-04-11 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2006-02-08 19:03:09 +00:00
|
|
|
foo::custom_type<int> x(1), y(2), z(1);
|
2005-04-11 22:26:01 +00:00
|
|
|
|
2006-02-08 19:03:09 +00:00
|
|
|
boost::hash<foo::custom_type<int> > hasher;
|
2005-04-11 22:26:01 +00:00
|
|
|
|
|
|
|
|
assert(hasher(x) == hasher(x));
|
|
|
|
|
assert(hasher(x) != hasher(y));
|
|
|
|
|
assert(hasher(x) == hasher(z));
|
2005-08-22 19:16:47 +00:00
|
|
|
|
|
|
|
|
return 0;
|
2005-04-11 22:26:01 +00:00
|
|
|
}
|