3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Setting Polices at Namespace Scope
</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=
"../pol_ref.html" title=
"Policy Reference">
9 <link rel=
"prev" href=
"policy_defaults.html" title=
"Using Macros to Change the Policy Defaults">
10 <link rel=
"next" href=
"pol_ref_ref.html" title=
"Policy Class Reference">
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=
"policy_defaults.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../pol_ref.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=
"pol_ref_ref.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.pol_ref.namespace_pol"></a><a class=
"link" href=
"namespace_pol.html" title=
"Setting Polices at Namespace Scope">Setting Polices at
29 </h3></div></div></div>
31 Sometimes what you really want to do is bring all the special functions,
32 or all the distributions into a specific namespace-scope, along with a specific
33 policy to use with them. There are two macros defined to assist with that:
35 <pre class=
"programlisting"><span class=
"identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS
</span><span class=
"special">(
</span><span class=
"identifier">Policy
</span><span class=
"special">)
</span>
40 <pre class=
"programlisting"><span class=
"identifier">BOOST_MATH_DECLARE_DISTRIBUTIONS
</span><span class=
"special">(
</span><span class=
"identifier">Type
</span><span class=
"special">,
</span> <span class=
"identifier">Policy
</span><span class=
"special">)
</span>
43 You can use either of these macros after including any special function or
44 distribution header. For example:
46 <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">special_functions
</span><span class=
"special">/
</span><span class=
"identifier">gamma
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
47 <span class=
"comment">//using boost::math::tgamma;
</span>
48 <span class=
"comment">// Need not declare using boost::math::tgamma here,
</span>
49 <span class=
"comment">// because will define tgamma in myspace using macro below.
</span>
51 <span class=
"keyword">namespace
</span> <span class=
"identifier">myspace
</span>
52 <span class=
"special">{
</span>
53 <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">policies
</span><span class=
"special">;
</span>
55 <span class=
"comment">// Define a policy that does not throw on overflow:
</span>
56 <span class=
"keyword">typedef
</span> <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">overflow_error
</span><span class=
"special"><</span><span class=
"identifier">errno_on_error
</span><span class=
"special">></span> <span class=
"special">></span> <span class=
"identifier">my_policy
</span><span class=
"special">;
</span>
58 <span class=
"comment">// Define the special functions in this scope to use the policy:
</span>
59 <span class=
"identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS
</span><span class=
"special">(
</span><span class=
"identifier">my_policy
</span><span class=
"special">)
</span>
60 <span class=
"special">}
</span>
62 <span class=
"comment">// Now we can use myspace::tgamma etc.
</span>
63 <span class=
"comment">// They will automatically use
"my_policy":
</span>
64 <span class=
"comment">//
</span>
65 <span class=
"keyword">double
</span> <span class=
"identifier">t
</span> <span class=
"special">=
</span> <span class=
"identifier">myspace
</span><span class=
"special">::
</span><span class=
"identifier">tgamma
</span><span class=
"special">(
</span><span class=
"number">30.0</span><span class=
"special">);
</span> <span class=
"comment">// Will *not* throw on overflow,
</span>
66 <span class=
"comment">// despite the large value of factorial
30 =
265252859812191058636308480000000</span>
67 <span class=
"comment">// unlike default policy boost::math::tgamma;
</span>
70 In this example, using BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS results in a
71 set of thin inline forwarding functions being defined:
73 <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>
74 <span class=
"keyword">inline
</span> <span class=
"identifier">T
</span> <span class=
"identifier">tgamma
</span><span class=
"special">(
</span><span class=
"identifier">T
</span> <span class=
"identifier">a
</span><span class=
"special">){
</span> <span class=
"keyword">return
</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">tgamma
</span><span class=
"special">(
</span><span class=
"identifier">a
</span><span class=
"special">,
</span> <span class=
"identifier">mypolicy
</span><span class=
"special">());
</span> <span class=
"special">}
</span>
76 <span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"keyword">class
</span> <span class=
"identifier">T
</span><span class=
"special">></span>
77 <span class=
"keyword">inline
</span> <span class=
"identifier">T
</span> <span class=
"identifier">lgamma
</span><span class=
"special">(
</span><span class=
"identifier">T
</span> <span class=
"identifier">a
</span><span class=
"special">)
</span> <span class=
"special">(
</span> <span class=
"keyword">return
</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">lgamma
</span><span class=
"special">(
</span><span class=
"identifier">a
</span><span class=
"special">,
</span> <span class=
"identifier">mypolicy
</span><span class=
"special">());
</span> <span class=
"special">}
</span>
80 and so on. Note that while a forwarding function is defined for all the special
81 functions, however, unless you include the specific header for the special
82 function you use (or boost/math/special_functions.hpp to include everything),
83 you will get linker errors from functions that are forward declared, but
87 We can do the same thing with the distributions, but this time we need to
88 specify the floating-point type to use:
90 <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">distributions
</span><span class=
"special">/
</span><span class=
"identifier">cauchy
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span>
92 <span class=
"keyword">namespace
</span> <span class=
"identifier">myspace
</span>
93 <span class=
"special">{
</span> <span class=
"comment">// using namespace boost::math::policies; // May be convenient in myspace.
</span>
95 <span class=
"comment">// Define a policy called my_policy to use.
</span>
96 <span class=
"keyword">using
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">policies
</span><span class=
"special">::
</span><span class=
"identifier">policy
</span><span class=
"special">;
</span>
98 <span class=
"comment">// In this case we want all the distribution accessor functions to compile,
</span>
99 <span class=
"comment">// even if they are mathematically undefined, so
</span>
100 <span class=
"comment">// make the policy assert_undefined.
</span>
101 <span class=
"keyword">using
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">policies
</span><span class=
"special">::
</span><span class=
"identifier">assert_undefined
</span><span class=
"special">;
</span>
103 <span class=
"keyword">typedef
</span> <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">assert_undefined
</span><span class=
"special"><</span><span class=
"keyword">false
</span><span class=
"special">></span> <span class=
"special">></span> <span class=
"identifier">my_policy
</span><span class=
"special">;
</span>
105 <span class=
"comment">// Finally apply this policy to type double.
</span>
106 <span class=
"identifier">BOOST_MATH_DECLARE_DISTRIBUTIONS
</span><span class=
"special">(
</span><span class=
"keyword">double
</span><span class=
"special">,
</span> <span class=
"identifier">my_policy
</span><span class=
"special">)
</span>
107 <span class=
"special">}
</span> <span class=
"comment">// namespace myspace
</span>
109 <span class=
"comment">// Now we can use myspace::cauchy etc, which will use policy
</span>
110 <span class=
"comment">// myspace::mypolicy:
</span>
111 <span class=
"comment">//
</span>
112 <span class=
"comment">// This compiles but throws a domain error exception at runtime.
</span>
113 <span class=
"comment">// Caution! If you omit the try'n'catch blocks,
</span>
114 <span class=
"comment">// it will just silently terminate, giving no clues as to why!
</span>
115 <span class=
"comment">// So try'n'catch blocks are very strongly recommended.
</span>
117 <span class=
"keyword">void
</span> <span class=
"identifier">test_cauchy
</span><span class=
"special">()
</span>
118 <span class=
"special">{
</span>
119 <span class=
"keyword">try
</span>
120 <span class=
"special">{
</span>
121 <span class=
"keyword">double
</span> <span class=
"identifier">d
</span> <span class=
"special">=
</span> <span class=
"identifier">mean
</span><span class=
"special">(
</span><span class=
"identifier">myspace
</span><span class=
"special">::
</span><span class=
"identifier">cauchy
</span><span class=
"special">());
</span> <span class=
"comment">// Cauchy does not have a mean!
</span>
122 <span class=
"special">}
</span>
123 <span class=
"keyword">catch
</span><span class=
"special">(
</span><span class=
"keyword">const
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">domain_error
</span><span class=
"special">&</span> <span class=
"identifier">e
</span><span class=
"special">)
</span>
124 <span class=
"special">{
</span>
125 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"identifier">e
</span><span class=
"special">.
</span><span class=
"identifier">what
</span><span class=
"special">()
</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
126 <span class=
"special">}
</span>
127 <span class=
"special">}
</span>
130 In this example the result of BOOST_MATH_DECLARE_DISTRIBUTIONS is to declare
131 a typedef for each distribution like this:
133 <pre class=
"programlisting"><span class=
"keyword">typedef
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">cauchy_distribution
</span><span class=
"special"><</span><span class=
"keyword">double
</span><span class=
"special">,
</span> <span class=
"identifier">my_policy
</span><span class=
"special">></span> <span class=
"identifier">cauchy
</span><span class=
"special">;
</span>
134 <span class=
"identifier">tyepdef
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">gamma_distribution
</span><span class=
"special"><</span><span class=
"keyword">double
</span><span class=
"special">,
</span> <span class=
"identifier">my_policy
</span><span class=
"special">></span> <span class=
"identifier">gamma
</span><span class=
"special">;
</span>
137 and so on. The name given to each typedef is the name of the distribution
138 with the
"_distribution" suffix removed.
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=
"policy_defaults.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../pol_ref.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=
"pol_ref_ref.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>