Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 1 | |
| 2 | // Copyright 2005-2009 Daniel James. |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/functional/hash.hpp> |
| 7 | #include <cassert> |
| 8 | |
| 9 | // This example illustrates how to customise ndnboost::hash portably, so that |
| 10 | // it'll work on both compilers that don't implement argument dependent lookup |
| 11 | // and compilers that implement strict two-phase template instantiation. |
| 12 | |
| 13 | namespace foo |
| 14 | { |
| 15 | template <class T> |
| 16 | class custom_type |
| 17 | { |
| 18 | T value; |
| 19 | public: |
| 20 | custom_type(T x) : value(x) {} |
| 21 | |
| 22 | std::size_t hash() const |
| 23 | { |
| 24 | ndnboost::hash<T> hasher; |
| 25 | return hasher(value); |
| 26 | } |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP |
| 31 | namespace ndnboost |
| 32 | #else |
| 33 | namespace foo |
| 34 | #endif |
| 35 | { |
| 36 | template <class T> |
| 37 | std::size_t hash_value(foo::custom_type<T> x) |
| 38 | { |
| 39 | return x.hash(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | int main() |
| 44 | { |
| 45 | foo::custom_type<int> x(1), y(2), z(1); |
| 46 | |
| 47 | ndnboost::hash<foo::custom_type<int> > hasher; |
| 48 | |
| 49 | assert(hasher(x) == hasher(x)); |
| 50 | assert(hasher(x) != hasher(y)); |
| 51 | assert(hasher(x) == hasher(z)); |
| 52 | |
| 53 | return 0; |
| 54 | } |