| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
|---|
| 4 | <title> Portability</title> |
|---|
| 5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
|---|
| 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.69.1"> |
|---|
| 7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries"> |
|---|
| 8 | <link rel="up" href="../hash.html" title="Chapter 5. Boost.Functional/Hash"> |
|---|
| 9 | <link rel="prev" href="combine.html" title=" Combining hash values"> |
|---|
| 10 | <link rel="next" href="reference_.html" title=" Reference"> |
|---|
| 11 | </head> |
|---|
| 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|---|
| 13 | <table cellpadding="2" width="100%"> |
|---|
| 14 | <td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../boost.png"></td> |
|---|
| 15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
|---|
| 16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
|---|
| 17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
|---|
| 18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
|---|
| 19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
|---|
| 20 | </table> |
|---|
| 21 | <hr> |
|---|
| 22 | <div class="spirit-nav"> |
|---|
| 23 | <a accesskey="p" href="combine.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="reference_.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 24 | </div> |
|---|
| 25 | <div class="section" lang="en"> |
|---|
| 26 | <div class="titlepage"><div><div><h3 class="title"> |
|---|
| 27 | <a name="hash.portability"></a> Portability</h3></div></div></div> |
|---|
| 28 | <p><code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> is written to be as portable as possible, but unfortunately, several |
|---|
| 29 | older compilers don't support argument dependent lookup (ADL) - the mechanism |
|---|
| 30 | used for customization. On those compilers custom overloads for hash_value |
|---|
| 31 | need to be declared in the boost namespace.</p> |
|---|
| 32 | <p> |
|---|
| 33 | On a strictly standards compliant compiler, an overload defined in the |
|---|
| 34 | boost namespace won't be found when <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> is instantiated, |
|---|
| 35 | so for these compilers the overload should only be declared in the same |
|---|
| 36 | namespace as the class.</p> |
|---|
| 37 | <p> |
|---|
| 38 | Let's say we have a simple custom type:</p> |
|---|
| 39 | <pre class="programlisting"><code class="literal"><span class="keyword">namespace</span><span class="identifier"> foo</span><span class="special"> |
|---|
| 40 | {</span><span class="keyword"> |
|---|
| 41 | struct</span><span class="identifier"> custom_type</span><span class="special"> |
|---|
| 42 | {</span><span class="keyword"> |
|---|
| 43 | int</span><span class="identifier"> value</span><span class="special">;</span><span class="keyword"> |
|---|
| 44 | |
|---|
| 45 | friend</span><span class="keyword"> inline</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">size_t</span><span class="identifier"> hash_value</span><span class="special">(</span><span class="identifier">custom_type</span><span class="identifier"> x</span><span class="special">)</span><span class="special"> |
|---|
| 46 | {</span> |
|---|
| 47 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="identifier"> hasher</span><span class="special">;</span><span class="keyword"> |
|---|
| 48 | return</span><span class="identifier"> hasher</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">value</span><span class="special">);</span><span class="special"> |
|---|
| 49 | }</span><span class="special"> |
|---|
| 50 | };</span><span class="special"> |
|---|
| 51 | }</span></code></pre> |
|---|
| 52 | <p> |
|---|
| 53 | On a compliant compiler, when <code class="computeroutput"><span class="identifier">hash_value</span></code> is called for this type, |
|---|
| 54 | it will look at the namespace inside the type and find <code class="computeroutput"><span class="identifier">hash_value</span></code> |
|---|
| 55 | but on a compiler which doesn't support ADL <code class="computeroutput"><span class="identifier">hash_value</span></code> won't be found.</p> |
|---|
| 56 | <p> |
|---|
| 57 | So on these compilers define a member function:</p> |
|---|
| 58 | <pre class="programlisting"><code class="literal"><span class="preprocessor">#ifndef</span><span class="identifier"> BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP</span><span class="keyword"> |
|---|
| 59 | friend</span><span class="keyword"> inline</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">size_t</span><span class="identifier"> hash_value</span><span class="special">(</span><span class="identifier">custom_type</span><span class="identifier"> x</span><span class="special">)</span><span class="special"> |
|---|
| 60 | {</span> |
|---|
| 61 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="identifier"> hasher</span><span class="special">;</span><span class="keyword"> |
|---|
| 62 | return</span><span class="identifier"> hasher</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">value</span><span class="special">);</span><span class="special"> |
|---|
| 63 | }</span><span class="preprocessor"> |
|---|
| 64 | #else</span><span class="identifier"> |
|---|
| 65 | std</span><span class="special">::</span><span class="identifier">size_t</span><span class="identifier"> hash</span><span class="special">()</span><span class="keyword"> const</span><span class="special"> |
|---|
| 66 | {</span> |
|---|
| 67 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="identifier"> hasher</span><span class="special">;</span><span class="keyword"> |
|---|
| 68 | return</span><span class="identifier"> hasher</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span><span class="special"> |
|---|
| 69 | }</span><span class="preprocessor"> |
|---|
| 70 | #endif</span></code></pre> |
|---|
| 71 | <p> |
|---|
| 72 | which will be called from the <code class="computeroutput"><span class="identifier">boost</span></code> namespace:</p> |
|---|
| 73 | <pre class="programlisting"><code class="literal"><span class="preprocessor">#ifdef</span><span class="identifier"> BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP</span><span class="keyword"> |
|---|
| 74 | namespace</span><span class="identifier"> boost</span><span class="special"> |
|---|
| 75 | {</span><span class="identifier"> |
|---|
| 76 | std</span><span class="special">::</span><span class="identifier">size_t</span><span class="identifier"> hash_value</span><span class="special">(</span><span class="identifier">foo</span><span class="special">::</span><span class="identifier">custom_type</span><span class="identifier"> x</span><span class="special">)</span><span class="special"> |
|---|
| 77 | {</span><span class="keyword"> |
|---|
| 78 | return</span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">hash</span><span class="special">();</span><span class="special"> |
|---|
| 79 | }</span><span class="special"> |
|---|
| 80 | }</span><span class="preprocessor"> |
|---|
| 81 | #endif</span></code></pre> |
|---|
| 82 | <p> |
|---|
| 83 | Full code for this example is at |
|---|
| 84 | <a href="../../../libs/functional/hash/examples/portable.cpp" target="_top">/libs/functional/hash/examples/portable.cpp</a>.</p> |
|---|
| 85 | <a name="portability.other_issues"></a><h2> |
|---|
| 86 | <a name="id2703933"></a>Other Issues</h2> |
|---|
| 87 | <p> |
|---|
| 88 | On Visual C++ versions 6.5 and 7.0, <code class="computeroutput"><span class="identifier">hash_value</span></code> isn't overloaded for built in |
|---|
| 89 | arrays. <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code>, <code class="computeroutput"><a href="../hash_combine.html" title="Function template hash_combine">boost::hash_combine</a></code> and <code class="computeroutput"><a href="../hash_range.html" title="Function hash_range">boost::hash_range</a></code> all use a workaround to |
|---|
| 90 | support built in arrays so this shouldn't be a problem in most cases.</p> |
|---|
| 91 | <p> |
|---|
| 92 | On Visual C++ versions 6.5 and 7.0, function pointers aren't currently supported.</p> |
|---|
| 93 | <p><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">hash_value</span><span class="special">(</span><span class="keyword">long</span><span class="keyword"> double</span><span class="special">)</span></code> on GCC on Solaris appears to treat |
|---|
| 94 | <code class="computeroutput"><span class="keyword">long</span><span class="keyword"> double</span></code>s as doubles - so the hash function doesn't take into account the |
|---|
| 95 | full range of values.</p> |
|---|
| 96 | </div> |
|---|
| 97 | <table width="100%"><tr> |
|---|
| 98 | <td align="left"></td> |
|---|
| 99 | <td align="right"><small>Copyright © 2005 Daniel James</small></td> |
|---|
| 100 | </tr></table> |
|---|
| 101 | <hr> |
|---|
| 102 | <div class="spirit-nav"> |
|---|
| 103 | <a accesskey="p" href="combine.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="reference_.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 104 | </div> |
|---|
| 105 | </body> |
|---|
| 106 | </html> |
|---|