Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/functional/hash/examples/portable.cpp @ 13

Last change on this file since 13 was 12, checked in by landauf, 18 years ago

added boost

File size: 1.3 KB
Line 
1
2//  Copyright Daniel James 2005. 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    struct custom_type
16    {
17        int value;
18
19        custom_type(int x) : value(x) {}
20
21#ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
22        friend inline std::size_t hash_value(custom_type x)
23        {
24            boost::hash<int> hasher;
25            return hasher(x.value);
26        }
27#else
28        std::size_t hash() const
29        {
30            boost::hash<int> hasher;
31            return hasher(value);
32        }
33#endif
34    };
35}
36
37#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
38namespace boost
39{
40    std::size_t hash_value(foo::custom_type x)
41    {
42        return x.hash();
43    }
44}
45#endif
46
47int main()
48{
49    foo::custom_type x(1), y(2), z(1);
50
51    boost::hash<foo::custom_type> hasher;
52
53    assert(hasher(x) == hasher(x));
54    assert(hasher(x) != hasher(y));
55    assert(hasher(x) == hasher(z));
56}
Note: See TracBrowser for help on using the repository browser.