Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/functional/hash/examples/portable.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 1.2 KB
Line 
1
2//  Copyright Daniel James 2005-2006. Use, modification, and distribution are
3//  subject to 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 boost::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
13namespace 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            boost::hash<T> hasher;
25            return hasher(value);
26        }
27    };
28}
29
30#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
31namespace boost
32#else
33namespace 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
43int main()
44{
45    foo::custom_type<int> x(1), y(2), z(1);
46
47    boost::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}
Note: See TracBrowser for help on using the repository browser.