3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Use in template code
</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.1">
8 <link rel=
"up" href=
"../tutorial.html" title=
"Tutorial">
9 <link rel=
"prev" href=
"non_templ.html" title=
"Use in non-template code">
10 <link rel=
"next" href=
"user_def.html" title=
"Use With User-Defined Types">
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>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"non_templ.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../tutorial.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=
"user_def.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h3 class=
"title">
27 <a name=
"math_toolkit.tutorial.templ"></a><a class=
"link" href=
"templ.html" title=
"Use in template code">Use in template code
</a>
28 </h3></div></div></div>
30 When using the constants inside a function template, we need to ensure that
31 we use a constant of the correct precision for our template parameters. We
32 can do this by calling the function-template versions,
<code class=
"computeroutput"><span class=
"identifier">pi
</span><span class=
"special"><</span><span class=
"identifier">FPType
</span><span class=
"special">>()
</span></code>, of the constants like this:
34 <pre class=
"programlisting"><span class=
"preprocessor">#include
</span> <span class=
"special"><</span><span class=
"identifier">boost
</span><span class=
"special">/
</span><span class=
"identifier">math
</span><span class=
"special">/
</span><span class=
"identifier">constants
</span><span class=
"special">/
</span><span class=
"identifier">constants
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
36 <span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"keyword">class
</span> <span class=
"identifier">Real
</span><span class=
"special">></span>
37 <span class=
"identifier">Real
</span> <span class=
"identifier">area
</span><span class=
"special">(
</span><span class=
"identifier">Real
</span> <span class=
"identifier">r
</span><span class=
"special">)
</span>
38 <span class=
"special">{
</span>
39 <span class=
"keyword">using
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">constants
</span><span class=
"special">;
</span>
40 <span class=
"keyword">return
</span> <span class=
"identifier">pi
</span><span class=
"special"><</span><span class=
"identifier">Real
</span><span class=
"special">>()
</span> <span class=
"special">*
</span> <span class=
"identifier">r
</span> <span class=
"special">*
</span> <span class=
"identifier">r
</span><span class=
"special">;
</span>
41 <span class=
"special">}
</span>
44 Although this syntax is a little less
"cute" than the non-template
45 version, the code is no less efficient (at least for the built-in types
46 <code class=
"computeroutput"><span class=
"keyword">float
</span></code>,
<code class=
"computeroutput"><span class=
"keyword">double
</span></code>
47 and
<code class=
"computeroutput"><span class=
"keyword">long
</span> <span class=
"keyword">double
</span></code>)
48 : the function template versions of the constants are simple inline functions
49 that return a constant of the correct precision for the type used. In addition,
50 these functions are declared
<code class=
"computeroutput"><span class=
"identifier">constexp
</span></code>
51 for those compilers that support this, allowing the result to be used in
52 constant-expressions provided the template argument is a literal type.
54 <div class=
"tip"><table border=
"0" summary=
"Tip">
56 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Tip]" src=
"../../../../../../doc/src/images/tip.png"></td>
57 <th align=
"left">Tip
</th>
59 <tr><td align=
"left" valign=
"top"><p>
60 Keep in mind the difference between the variable version, just
<code class=
"computeroutput"><span class=
"identifier">pi
</span></code>, and the template-function version:
61 the template-function requires both a
<<em class=
"replaceable"><code>floating-point-type
</code></em>>
62 and function call
<code class=
"computeroutput"><span class=
"special">()
</span></code> brackets,
63 for example:
<code class=
"computeroutput"><span class=
"identifier">pi
</span><span class=
"special"><</span><span class=
"keyword">double
</span><span class=
"special">>()
</span></code>.
64 You cannot write
<code class=
"computeroutput"><span class=
"keyword">double
</span> <span class=
"identifier">p
</span>
65 <span class=
"special">=
</span> <span class=
"identifier">pi
</span><span class=
"special"><>()
</span></code>, nor
<code class=
"computeroutput"><span class=
"keyword">double
</span>
66 <span class=
"identifier">p
</span> <span class=
"special">=
</span>
67 <span class=
"identifier">pi
</span><span class=
"special">()
</span></code>.
70 <div class=
"note"><table border=
"0" summary=
"Note">
72 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Note]" src=
"../../../../../../doc/src/images/note.png"></td>
73 <th align=
"left">Note
</th>
75 <tr><td align=
"left" valign=
"top">
77 You can always use
<span class=
"bold"><strong>both
</strong></span> variable and template-function
78 versions
<span class=
"bold"><strong>provided calls are fully qualified
</strong></span>,
81 <pre class=
"programlisting"><span class=
"keyword">double
</span> <span class=
"identifier">my_pi1
</span> <span class=
"special">=
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">constants
</span><span class=
"special">::
</span><span class=
"identifier">pi
</span><span class=
"special"><</span><span class=
"keyword">double
</span><span class=
"special">>();
</span>
82 <span class=
"keyword">double
</span> <span class=
"identifier">my_pi2
</span> <span class=
"special">=
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">double_constants
</span><span class=
"special">::
</span><span class=
"identifier">pi
</span><span class=
"special">;
</span>
86 <div class=
"warning"><table border=
"0" summary=
"Warning">
88 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Warning]" src=
"../../../../../../doc/src/images/warning.png"></td>
89 <th align=
"left">Warning
</th>
91 <tr><td align=
"left" valign=
"top">
93 It may be tempting to simply define
95 <pre class=
"programlisting"><span class=
"keyword">using
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">double_constants
</span><span class=
"special">;
</span>
96 <span class=
"keyword">using
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">constants
</span><span class=
"special">;
</span>
99 but if you do define two namespaces, this will, of course, create ambiguity!
101 <pre class=
"programlisting"><span class=
"keyword">double
</span> <span class=
"identifier">my_pi
</span> <span class=
"special">=
</span> <span class=
"identifier">pi
</span><span class=
"special">();
</span> <span class=
"comment">// error C2872: 'pi' : ambiguous symbol
</span>
102 <span class=
"keyword">double
</span> <span class=
"identifier">my_pi2
</span> <span class=
"special">=
</span> <span class=
"identifier">pi
</span><span class=
"special">;
</span> <span class=
"comment">// Context does not allow for disambiguation of overloaded function
</span>
105 Although the mistake above is fairly obvious, it is also not too difficult
106 to do this accidentally, or worse, create it in someone elses code.
109 Therefore is it prudent to avoid this risk by
<span class=
"bold"><strong>localising
110 the scope of such definitions
</strong></span>, as shown above.
114 <div class=
"tip"><table border=
"0" summary=
"Tip">
116 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Tip]" src=
"../../../../../../doc/src/images/tip.png"></td>
117 <th align=
"left">Tip
</th>
119 <tr><td align=
"left" valign=
"top">
121 Be very careful with the type provided as parameter. For example, providing
122 an
<span class=
"bold"><strong>integer
</strong></span> instead of a floating-point
123 type can be disastrous (a C++ feature).
125 <pre class=
"programlisting"><span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"Area = "</span> <span class=
"special"><<</span> <span class=
"identifier">area
</span><span class=
"special">(
</span><span class=
"number">2</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span> <span class=
"comment">// Area =
12!!!
</span></pre>
127 You should get a compiler warning
129 <pre class=
"programlisting">warning : 'return' : conversion from 'double' to 'int', possible loss of data
132 Failure to heed this warning can lead to very wrong answers!
135 You can also avoid this by being explicit about the type of
<code class=
"computeroutput"><span class=
"identifier">Area
</span></code>.
137 <pre class=
"programlisting"><span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"Area = "</span> <span class=
"special"><<</span> <span class=
"identifier">area
</span><span class=
"special"><</span><span class=
"keyword">double
</span><span class=
"special">>(
</span><span class=
"number">2</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span> <span class=
"comment">// Area =
12.566371</span></pre>
141 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
142 <td align=
"left"></td>
143 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2006-
2010,
2012-
2014 Nikhar Agrawal,
144 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
145 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
146 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
147 Distributed under the Boost Software License, Version
1.0. (See accompanying
148 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>)
153 <div class=
"spirit-nav">
154 <a accesskey=
"p" href=
"non_templ.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../tutorial.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=
"user_def.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>