<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Portability</title> <link rel="stylesheet" href="../boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> <link rel="up" href="../hash.html" title="Chapter 8. Boost.Functional/Hash"> <link rel="prev" href="combine.html" title="Combining hash values"> <link rel="next" href="disable.html" title="Disabling The Extensions"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> <td align="center"><a href="../../../index.html">Home</a></td> <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="combine.html"><img src="../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="disable.html"><img src="../../../doc/html/images/next.png" alt="Next"></a> </div> <div class="section" title="Portability"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="hash.portability"></a><a class="link" href="portability.html" title="Portability"> Portability</a> </h2></div></div></div> <p> <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> is written to be as portable as possible, but unfortunately, several older compilers don't support argument dependent lookup (ADL) - the mechanism used for customisation. On those compilers custom overloads for <code class="computeroutput"><span class="identifier">hash_value</span></code> needs to be declared in the boost namespace. </p> <p> On a strictly standards compliant compiler, an overload defined in the boost namespace won't be found when <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> is instantiated, so for these compilers the overload should only be declared in the same namespace as the class. </p> <p> Let's say we have a simple custom type: </p> <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">foo</span> <span class="special">{</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> <span class="keyword">class</span> <span class="identifier">custom_type</span> <span class="special">{</span> <span class="identifier">T</span> <span class="identifier">value</span><span class="special">;</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">custom_type</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">value</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span> <span class="keyword">friend</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">{</span> <code class="computeroutput"><a class="link" 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">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">}</span> <span class="special">};</span> <span class="special">}</span> </pre> <p> On a compliant compiler, when <code class="computeroutput"><span class="identifier">hash_value</span></code> is called for this type, it will look at the namespace inside the type and find <code class="computeroutput"><span class="identifier">hash_value</span></code> but on a compiler which doesn't support ADL <code class="computeroutput"><span class="identifier">hash_value</span></code> won't be found. To make things worse, some compilers which do support ADL won't find a friend class defined inside the class. </p> <p> So first move the member function out of the class: </p> <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">foo</span> <span class="special">{</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> <span class="keyword">class</span> <span class="identifier">custom_type</span> <span class="special">{</span> <span class="identifier">T</span> <span class="identifier">value</span><span class="special">;</span> <span class="keyword">public</span><span class="special">:</span> <span class="identifier">custom_type</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">value</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">hash</span><span class="special">(</span><span class="identifier">custom_type</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">hasher</span><span class="special">;</span> <span class="keyword">return</span> <span class="identifier">hasher</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span> <span class="special">}</span> <span class="special">};</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></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="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">hash</span><span class="special">();</span> <span class="special">}</span> <span class="special">}</span> </pre> <p> Unfortunately, I couldn't declare hash_value as a friend, as some compilers don't support template friends, so instead I declared a member function to calculate the hash, and called it from hash_value. </p> <p> For compilers which don't support ADL, hash_value needs to be defined in the boost namespace: </p> <pre class="programlisting"><span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP</span> <span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="preprocessor">#else</span> <span class="keyword">namespace</span> <span class="identifier">foo</span> <span class="preprocessor">#endif</span> <span class="special">{</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></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">foo</span><span class="special">::</span><span class="identifier">custom_type</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">hash</span><span class="special">();</span> <span class="special">}</span> <span class="special">}</span> </pre> <p> Full code for this example is at <a href="../../../libs/functional/hash/examples/portable.cpp" target="_top">/libs/functional/hash/examples/portable.cpp</a>. </p> <a name="hash.portability.other_issues"></a><h3> <a name="id882513"></a> <a class="link" href="portability.html#hash.portability.other_issues">Other Issues</a> </h3> <p> 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 arrays. <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code>, <code class="computeroutput"><a class="link" href="reference.html#boost.hash_combine">boost::hash_combine</a></code> and <code class="computeroutput"><a class="link" href="reference.html#boost.hash_range">boost::hash_range</a></code> all use a workaround to support built in arrays so this shouldn't be a problem in most cases. </p> <p> On Visual C++ versions 6.5 and 7.0, function pointers aren't currently supported. </p> <p> When using GCC on Solaris, <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> treats <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>s as <code class="computeroutput"><span class="keyword">double</span></code>s - so the hash function doesn't take into account the full range of values. </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright © 2005-2008 Daniel James<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="combine.html"><img src="../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="disable.html"><img src="../../../doc/html/images/next.png" alt="Next"></a> </div> </body> </html>