]>
Commit | Line | Data |
---|---|---|
1 | <html> | |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Overloading template functions with float128_t</title> | |
5 | <link rel="stylesheet" href="../../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../index.html" title="Math Toolkit 2.5.0"> | |
8 | <link rel="up" href="../float128.html" title="Implementation of Float128 type"> | |
9 | <link rel="prev" href="../float128.html" title="Implementation of Float128 type"> | |
10 | <link rel="next" href="exp_function.html" title="Exponential function"> | |
11 | </head> | |
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> | |
13 | <table cellpadding="2" width="100%"><tr> | |
14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> | |
15 | <td align="center"><a href="../../../../../../index.html">Home</a></td> | |
16 | <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> | |
17 | <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> | |
18 | <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> | |
19 | <td align="center"><a href="../../../../../../more/index.htm">More</a></td> | |
20 | </tr></table> | |
21 | <hr> | |
22 | <div class="spirit-nav"> | |
23 | <a accesskey="p" href="../float128.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../float128.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exp_function.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h3 class="title"> | |
27 | <a name="math_toolkit.float128.overloading_template_functions_w"></a><a class="link" href="overloading_template_functions_w.html" title="Overloading template functions with float128_t">Overloading | |
28 | template functions with float128_t</a> | |
29 | </h3></div></div></div> | |
30 | <p> | |
31 | An artifact of providing C++ standard library support for quadmath may mandate | |
32 | the inclusion of <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> | |
33 | <span class="bold"><strong>before</strong></span> the inclusion of other headers. | |
34 | </p> | |
35 | <p> | |
36 | Consider a function that calls <code class="computeroutput"><span class="identifier">fabs</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span></code> | |
37 | and has previously injected <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">fabs</span><span class="special">()</span></code> into local scope via a <code class="computeroutput"><span class="keyword">using</span></code> | |
38 | directive: | |
39 | </p> | |
40 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
41 | <span class="keyword">bool</span> <span class="identifier">unsigned_compare</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">b</span><span class="special">)</span> | |
42 | <span class="special">{</span> | |
43 | <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">fabs</span><span class="special">;</span> | |
44 | <span class="keyword">return</span> <span class="identifier">fabs</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">fabs</span><span class="special">(</span><span class="identifier">b</span><span class="special">);</span> | |
45 | <span class="special">}</span> | |
46 | </pre> | |
47 | <p> | |
48 | In this function, the correct overload of <code class="computeroutput"><span class="identifier">fabs</span></code> | |
49 | may be found via <a href="http://en.wikipedia.org/wiki/Argument-dependent_name_lookup" target="_top">argument-dependent-lookup | |
50 | (ADL)</a> or by calling one of the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">fabs</span></code> | |
51 | overloads. There is a key difference between them however: an overload in | |
52 | the same namespace as T and found via ADL need <span class="emphasis"><em><span class="bold"><strong>not | |
53 | be defined at the time the function is declared</strong></span></em></span>. However, | |
54 | all the types declared in <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> | |
55 | are fundamental types, so for these types we are relying on finding an overload | |
56 | declared in namespace <code class="computeroutput"><span class="identifier">std</span></code>. | |
57 | In that case however, <span class="emphasis"><em><span class="bold"><strong>all such overloads | |
58 | must be declared prior to the definition of function <code class="computeroutput"><span class="identifier">unsigned_compare</span></code> | |
59 | otherwise they are not considered</strong></span></em></span>. | |
60 | </p> | |
61 | <p> | |
62 | In the event that <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> | |
63 | has been included <span class="bold"><strong>after</strong></span> the definition of | |
64 | the above function, the correct overload of <code class="computeroutput"><span class="identifier">fabs</span></code>, | |
65 | while present, is simply not considered as part of the overload set. So the | |
66 | compiler tries to downcast the <code class="computeroutput"><span class="identifier">float128_t</span></code> | |
67 | argument first to <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>, | |
68 | then to <code class="computeroutput"><span class="keyword">double</span></code>, then to <code class="computeroutput"><span class="keyword">float</span></code>; the compilation fails because the result | |
69 | is ambiguous. However the compiler error message will appear cruelly inscrutable, | |
70 | at an apparently irelevant line number and making no mention of <code class="computeroutput"><span class="identifier">float128</span></code>: the word <span class="emphasis"><em>ambiguous</em></span> | |
71 | is the clue to what is wrong. | |
72 | </p> | |
73 | <p> | |
74 | Provided you <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> | |
75 | <span class="bold"><strong>before</strong></span> the inclusion of the any header containing | |
76 | generic floating point code (such as other Boost.Math headers, then the compiler | |
77 | will know about and use the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">fabs</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">float128_t</span><span class="special">)</span></code> | |
78 | that we provide in <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>. | |
79 | </p> | |
80 | </div> | |
81 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
82 | <td align="left"></td> | |
83 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
84 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
85 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
86 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
87 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
88 | 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>) | |
89 | </p> | |
90 | </div></td> | |
91 | </tr></table> | |
92 | <hr> | |
93 | <div class="spirit-nav"> | |
94 | <a accesskey="p" href="../float128.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../float128.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exp_function.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
95 | </div> | |
96 | </body> | |
97 | </html> |