1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Chapter 1. Boost.Functional/Factory 1.0</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="index.html" title="Chapter 1. Boost.Functional/Factory 1.0"> 8</head> 9<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 10<table cellpadding="2" width="100%"><tr> 11<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 12<td align="center"><a href="../../../../../index.html">Home</a></td> 13<td align="center"><a href="../../../../libraries.htm">Libraries</a></td> 14<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 15<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 16<td align="center"><a href="../../../../../more/index.htm">More</a></td> 17</tr></table> 18<hr> 19<div class="spirit-nav"></div> 20<div class="chapter"> 21<div class="titlepage"><div> 22<div><h2 class="title"> 23<a name="boost_functional_factory"></a>Chapter 1. Boost.Functional/Factory 1.0</h2></div> 24<div><div class="author"><h3 class="author"> 25<span class="firstname">Tobias</span> <span class="surname">Schwinger</span> 26</h3></div></div> 27<div><div class="author"><h3 class="author"> 28<span class="firstname">Glen</span> <span class="surname">Fernandes</span> 29</h3></div></div> 30<div><p class="copyright">Copyright © 2007, 2008 Tobias Schwinger</p></div> 31<div><p class="copyright">Copyright © 2019 Glen Joseph Fernandes</p></div> 32<div><div class="legalnotice"> 33<a name="boost_functional_factory.legal"></a><p> 34 Distributed under the Boost Software License, Version 1.0. 35 </p> 36</div></div> 37</div></div> 38<div class="toc"> 39<p><b>Table of Contents</b></p> 40<dl class="toc"> 41<dt><span class="section"><a href="index.html#boost_functional_factory.brief_description">Brief Description</a></span></dt> 42<dt><span class="section"><a href="index.html#boost_functional_factory.background">Background</a></span></dt> 43<dt><span class="section"><a href="index.html#boost_functional_factory.reference">Reference</a></span></dt> 44<dt><span class="section"><a href="index.html#boost_functional_factory.changes">Changes</a></span></dt> 45<dt><span class="section"><a href="index.html#boost_functional_factory.acknowledgements">Acknowledgements</a></span></dt> 46<dt><span class="section"><a href="index.html#boost_functional_factory.references">References</a></span></dt> 47</dl> 48</div> 49<div class="section"> 50<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 51<a name="boost_functional_factory.brief_description"></a><a class="link" href="index.html#boost_functional_factory.brief_description" title="Brief Description">Brief Description</a> 52</h2></div></div></div> 53<p> 54 The template <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">factory</span></code> lets you encapsulate a <code class="computeroutput"><span class="keyword">new</span></code> expression as a function object, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">value_factory</span></code> 55 encapsulates a constructor invocation without <code class="computeroutput"><span class="keyword">new</span></code>. 56 </p> 57<p> 58</p> 59<pre class="programlisting"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">factory</span></code><span class="special"><</span><span class="identifier">T</span><span class="special">*>()(</span><span class="identifier">arg1</span><span class="special">,</span><span class="identifier">arg2</span><span class="special">,</span><span class="identifier">arg3</span><span class="special">)</span> 60<span class="comment">// same as new T(arg1,arg2,arg3)</span> 61 62<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">value_factory</span></code><span class="special"><</span><span class="identifier">T</span><span class="special">>()(</span><span class="identifier">arg1</span><span class="special">,</span><span class="identifier">arg2</span><span class="special">,</span><span class="identifier">arg3</span><span class="special">)</span> 63<span class="comment">// same as T(arg1,arg2,arg3)</span> 64</pre> 65<p> 66 </p> 67<p> 68 Before C++11 the arguments to the function objects have to be LValues. A factory 69 that also accepts RValues can be composed using the <a href="http://www.boost.org/libs/functional/forward/doc/index.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward_adapter</span></code></a> 70 or <a href="http://www.boost.org/libs/bind/bind.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span></code></a>. 71 In C++11 or higher the arguments can be LValues or RValues. 72 </p> 73</div> 74<div class="section"> 75<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 76<a name="boost_functional_factory.background"></a><a class="link" href="index.html#boost_functional_factory.background" title="Background">Background</a> 77</h2></div></div></div> 78<p> 79 In traditional Object Oriented Programming a Factory is an object implementing 80 an interface of one or more methods that construct objects conforming to known 81 interfaces. 82 </p> 83<p> 84</p> 85<pre class="programlisting"><span class="comment">// assuming a_concrete_class and another_concrete_class are derived</span> 86<span class="comment">// from an_abstract_class</span> 87 88<span class="keyword">struct</span> <span class="identifier">a_factory</span> <span class="special">{</span> 89 <span class="keyword">virtual</span> <span class="identifier">an_abstract_class</span><span class="special">*</span> <span class="identifier">create</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 90 <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">a_factory</span><span class="special">()</span> <span class="special">{</span> <span class="special">}</span> 91<span class="special">};</span> 92 93<span class="keyword">struct</span> <span class="identifier">a_concrete_factory</span> 94 <span class="special">:</span> <span class="identifier">a_factory</span> <span class="special">{</span> 95 <span class="identifier">an_abstract_class</span><span class="special">*</span> <span class="identifier">create</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> 96 <span class="keyword">return</span> <span class="keyword">new</span> <span class="identifier">a_concrete_class</span><span class="special">();</span> 97 <span class="special">}</span> 98<span class="special">};</span> 99 100<span class="keyword">struct</span> <span class="identifier">another_concrete_factory</span> 101 <span class="special">:</span> <span class="identifier">a_factory</span> <span class="special">{</span> 102 <span class="identifier">an_abstract_class</span><span class="special">*</span> <span class="identifier">create</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> 103 <span class="keyword">return</span> <span class="keyword">new</span> <span class="identifier">another_concrete_class</span><span class="special">();</span> 104 <span class="special">}</span> 105<span class="special">};</span> 106 107<span class="comment">// [...]</span> 108 109<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 110<span class="special">{</span> 111 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ptr_map</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">a_factory</span><span class="special">></span> <span class="identifier">factories</span><span class="special">;</span> 112 113 <span class="comment">// [...]</span> 114 115 <span class="identifier">factories</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="string">"a_name"</span><span class="special">,</span> 116 <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special"><</span><span class="identifier">a_factory</span><span class="special">>(</span><span class="keyword">new</span> <span class="identifier">a_concrete_factory</span><span class="special">));</span> 117 <span class="identifier">factories</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="string">"another_name"</span><span class="special">,</span> 118 <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special"><</span><span class="identifier">a_factory</span><span class="special">>(</span><span class="keyword">new</span> <span class="identifier">another_concrete_factory</span><span class="special">));</span> 119 120 <span class="comment">// [...]</span> 121 122 <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special"><</span><span class="identifier">an_abstract_class</span><span class="special">></span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">factories</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">some_name</span><span class="special">).</span><span class="identifier">create</span><span class="special">());</span> 123 124 <span class="comment">// [...]</span> 125<span class="special">}</span> 126</pre> 127<p> 128 </p> 129<p> 130 This approach has several drawbacks. The most obvious one is that there is 131 lots of boilerplate code. In other words there is too much code to express 132 a rather simple intention. We could use templates to get rid of some of it 133 but the approach remains inflexible: 134 </p> 135<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 136<li class="listitem"> 137 We may want a factory that takes some arguments that are forwarded to the 138 constructor, 139 </li> 140<li class="listitem"> 141 we will probably want to use smart pointers, 142 </li> 143<li class="listitem"> 144 we may want several member functions to create different kinds of objects, 145 </li> 146<li class="listitem"> 147 we might not necessarily need a polymorphic base class for the objects, 148 </li> 149<li class="listitem"> 150 as we will see, we do not need a factory base class at all, 151 </li> 152<li class="listitem"> 153 we might want to just call the constructor - without <code class="computeroutput"><span class="keyword">new</span></code> 154 to create an object on the stack, and 155 </li> 156<li class="listitem"> 157 finally we might want to use customized memory management. 158 </li> 159</ul></div> 160<p> 161 Experience has shown that using function objects and generic Boost components 162 for their composition, Design Patterns that describe callback mechanisms (typically 163 requiring a high percentage of boilerplate code with pure Object Oriented methodology) 164 become implementable with just few code lines and without extra classes. 165 </p> 166<p> 167 Factories are callback mechanisms for constructors, so we provide two class 168 templates, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">value_factory</span></code> and <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">factory</span></code>, 169 that encapsulate object construction via direct application of the constructor 170 and the <code class="computeroutput"><span class="keyword">new</span></code> operator, respectively. 171 </p> 172<p> 173 We let the function objects forward their arguments to the construction expressions 174 they encapsulate. Over this <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">factory</span></code> 175 optionally allows the use of smart pointers and <a href="https://www.boost.org/sgi/stl/Allocators.html" target="_top">Allocators</a>. 176 </p> 177<p> 178 Compile-time polymorphism can be used where appropriate, 179 </p> 180<p> 181</p> 182<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> 183<span class="keyword">void</span> <span class="identifier">do_something</span><span class="special">()</span> 184<span class="special">{</span> 185 <span class="comment">// [...]</span> 186 <span class="identifier">T</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">);</span> 187 188 <span class="comment">// for conceptually similar objects x we neither need virtual</span> 189 <span class="comment">// functions nor a common base class in this context.</span> 190 <span class="comment">// [...]</span> 191<span class="special">}</span> 192</pre> 193<p> 194 </p> 195<p> 196 Now, to allow inhomogeneous signatures for the constructors of the types passed 197 in for <code class="computeroutput"><span class="identifier">T</span></code> we can use <code class="computeroutput"><span class="identifier">value_factory</span></code> and <a href="http://www.boost.org/libs/bind/bind.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span></code></a> 198 to normalize between them. 199 </p> 200<p> 201</p> 202<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">ValueFactory</span><span class="special">></span> 203<span class="keyword">void</span> <span class="identifier">do_something</span><span class="special">(</span><span class="identifier">ValueFactory</span> <span class="identifier">make_obj</span> <span class="special">=</span> <span class="identifier">ValueFactory</span><span class="special">())</span> 204<span class="special">{</span> 205 <span class="comment">// [...]</span> 206 <span class="keyword">typename</span> <span class="identifier">ValueFactory</span><span class="special">::</span><span class="identifier">result_type</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">make_obj</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">);</span> 207 208 <span class="comment">// for conceptually similar objects x we neither need virtual</span> 209 <span class="comment">// functions nor a common base class in this context.</span> 210 <span class="comment">// [...]</span> 211<span class="special">}</span> 212 213<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 214<span class="special">{</span> 215 <span class="comment">// [...]</span> 216 217 <span class="identifier">do_something</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">value_factory</span><span class="special"><</span><span class="identifier">X</span><span class="special">>());</span> 218 <span class="identifier">do_something</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">value_factory</span><span class="special"><</span><span class="identifier">Y</span><span class="special">>(),</span> <span class="identifier">_1</span><span class="special">,</span> <span class="number">5</span><span class="special">,</span> <span class="identifier">_2</span><span class="special">));</span> 219 <span class="comment">// construct X(a, b) and Y(a, 5, b), respectively.</span> 220 221 <span class="comment">// [...]</span> 222<span class="special">}</span> 223</pre> 224<p> 225 </p> 226<p> 227 Maybe we want our objects to outlive the function's scope, in this case we 228 have to use dynamic allocation; 229 </p> 230<p> 231</p> 232<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Factory</span><span class="special">></span> 233<span class="identifier">whatever</span> <span class="identifier">do_something</span><span class="special">(</span><span class="identifier">Factory</span> <span class="identifier">new_obj</span> <span class="special">=</span> <span class="identifier">Factory</span><span class="special">())</span> 234<span class="special">{</span> 235 <span class="keyword">typename</span> <span class="identifier">Factory</span><span class="special">::</span><span class="identifier">result_type</span> <span class="identifier">ptr</span> <span class="special">=</span> <span class="identifier">new_obj</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">);</span> 236 237 <span class="comment">// again, no common base class or virtual functions needed,</span> 238 <span class="comment">// we could enforce a polymorphic base by writing e.g.</span> 239 <span class="comment">// boost::shared_ptr<base></span> 240 <span class="comment">// instead of</span> 241 <span class="comment">// typename Factory::result_type</span> 242 <span class="comment">// above.</span> 243 <span class="comment">// Note that we are also free to have the type erasure happen</span> 244 <span class="comment">// somewhere else (e.g. in the constructor of this function's</span> 245 <span class="comment">// result type).</span> 246 247 <span class="comment">// [...]</span> 248<span class="special">}</span> 249 250<span class="comment">// [... call do_something like above but with boost::factory instead</span> 251<span class="comment">// of boost::value_factory]</span> 252</pre> 253<p> 254 </p> 255<p> 256 Although we might have created polymorphic objects in the previous example, 257 we have used compile time polymorphism for the factory. If we want to erase 258 the type of the factory and thus allow polymorphism at run time, we can use 259 <a href="http://www.boost.org/doc/html/function.html" target="_top">Boost.Function</a> 260 to do so. The first example can be rewritten as follows. 261 </p> 262<p> 263</p> 264<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span><span class="special"><</span><span class="identifier">an_abstract_class</span><span class="special">*()></span> <span class="identifier">a_factory</span><span class="special">;</span> 265 266<span class="comment">// [...]</span> 267 268<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 269<span class="special">{</span> 270 <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">a_factory</span><span class="special">></span> <span class="identifier">factories</span><span class="special">;</span> 271 272 <span class="comment">// [...]</span> 273 274 <span class="identifier">factories</span><span class="special">[</span><span class="string">"a_name"</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">factory</span><span class="special"><</span><span class="identifier">a_concrete_class</span><span class="special">*>();</span> 275 <span class="identifier">factories</span><span class="special">[</span><span class="string">"another_name"</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">factory</span><span class="special"><</span><span class="identifier">another_concrete_class</span><span class="special">*>();</span> 276 277 <span class="comment">// [...]</span> 278<span class="special">}</span> 279</pre> 280<p> 281 </p> 282<p> 283 Of course we can just as easy create factories that take arguments and/or return 284 <a href="http://www.boost.org/libs/smart_ptr/index.html" target="_top">Smart Pointers</a>. 285 </p> 286</div> 287<div class="section"> 288<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 289<a name="boost_functional_factory.reference"></a><a class="link" href="index.html#boost_functional_factory.reference" title="Reference">Reference</a> 290</h2></div></div></div> 291<div class="toc"><dl class="toc"> 292<dt><span class="section"><a href="index.html#boost_functional_factory.reference.value_factory">value_factory</a></span></dt> 293<dt><span class="section"><a href="index.html#boost_functional_factory.reference.factory">factory</a></span></dt> 294</dl></div> 295<div class="section"> 296<div class="titlepage"><div><div><h3 class="title"> 297<a name="boost_functional_factory.reference.value_factory"></a><a class="link" href="index.html#boost_functional_factory.reference.value_factory" title="value_factory">value_factory</a> 298</h3></div></div></div> 299<h5> 300<a name="boost_functional_factory.reference.value_factory.h0"></a> 301 <span class="phrase"><a name="boost_functional_factory.reference.value_factory.description"></a></span><a class="link" href="index.html#boost_functional_factory.reference.value_factory.description">Description</a> 302 </h5> 303<p> 304 Function object template that invokes the constructor of the type <code class="computeroutput"><span class="identifier">T</span></code>. 305 </p> 306<h5> 307<a name="boost_functional_factory.reference.value_factory.h1"></a> 308 <span class="phrase"><a name="boost_functional_factory.reference.value_factory.header"></a></span><a class="link" href="index.html#boost_functional_factory.reference.value_factory.header">Header</a> 309 </h5> 310<p> 311</p> 312<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">functional</span><span class="special">/</span><span class="identifier">value_factory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 313</pre> 314<p> 315 </p> 316<h5> 317<a name="boost_functional_factory.reference.value_factory.h2"></a> 318 <span class="phrase"><a name="boost_functional_factory.reference.value_factory.synopsis"></a></span><a class="link" href="index.html#boost_functional_factory.reference.value_factory.synopsis">Synopsis</a> 319 </h5> 320<p> 321</p> 322<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> 323 324<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> 325<span class="keyword">class</span> <span class="identifier">value_factory</span><span class="special">;</span> 326 327<span class="special">}</span> <span class="comment">// boost</span> 328</pre> 329<p> 330 </p> 331<div class="variablelist"> 332<p class="title"><b>Notation</b></p> 333<dl class="variablelist"> 334<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span></code></span></dt> 335<dd><p> 336 an arbitrary type with at least one public constructor 337 </p></dd> 338<dt><span class="term"><code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code></span></dt> 339<dd><p> 340 argument values to a constructor of <code class="computeroutput"><span class="identifier">T</span></code> 341 </p></dd> 342<dt><span class="term"><code class="computeroutput"><span class="identifier">F</span></code></span></dt> 343<dd><p> 344 the type <code class="computeroutput"><span class="identifier">value_factory</span><span class="special"><</span><span class="identifier">F</span><span class="special">></span></code> 345 </p></dd> 346<dt><span class="term"><code class="computeroutput"><span class="identifier">f</span></code></span></dt> 347<dd><p> 348 an instance object of <code class="computeroutput"><span class="identifier">F</span></code> 349 </p></dd> 350</dl> 351</div> 352<h5> 353<a name="boost_functional_factory.reference.value_factory.h3"></a> 354 <span class="phrase"><a name="boost_functional_factory.reference.value_factory.expression_semantics"></a></span><a class="link" href="index.html#boost_functional_factory.reference.value_factory.expression_semantics">Expression 355 Semantics</a> 356 </h5> 357<div class="informaltable"><table class="table"> 358<colgroup> 359<col> 360<col> 361</colgroup> 362<thead><tr> 363<th> 364 <p> 365 Expression 366 </p> 367 </th> 368<th> 369 <p> 370 Semantics 371 </p> 372 </th> 373</tr></thead> 374<tbody> 375<tr> 376<td> 377 <p> 378 <code class="computeroutput"><span class="identifier">F</span><span class="special">()</span></code> 379 </p> 380 </td> 381<td> 382 <p> 383 creates an object of type <code class="computeroutput"><span class="identifier">F</span></code>. 384 </p> 385 </td> 386</tr> 387<tr> 388<td> 389 <p> 390 <code class="computeroutput"><span class="identifier">F</span><span class="special">(</span><span class="identifier">f</span><span class="special">)</span></code> 391 </p> 392 </td> 393<td> 394 <p> 395 creates an object of type <code class="computeroutput"><span class="identifier">F</span></code>. 396 </p> 397 </td> 398</tr> 399<tr> 400<td> 401 <p> 402 <code class="computeroutput"><span class="identifier">f</span><span class="special">(</span><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span><span class="special">)</span></code> 403 </p> 404 </td> 405<td> 406 <p> 407 returns <code class="computeroutput"><span class="identifier">T</span><span class="special">(</span><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span><span class="special">)</span></code>. 408 </p> 409 </td> 410</tr> 411<tr> 412<td> 413 <p> 414 <code class="computeroutput"><span class="identifier">F</span><span class="special">::</span><span class="identifier">result_type</span></code> 415 </p> 416 </td> 417<td> 418 <p> 419 is the type <code class="computeroutput"><span class="identifier">T</span></code>. 420 </p> 421 </td> 422</tr> 423</tbody> 424</table></div> 425<h5> 426<a name="boost_functional_factory.reference.value_factory.h4"></a> 427 <span class="phrase"><a name="boost_functional_factory.reference.value_factory.limits"></a></span><a class="link" href="index.html#boost_functional_factory.reference.value_factory.limits">Limits</a> 428 </h5> 429<p> 430 Before C++11, the maximum number of arguments supported is 10. Since C++11 431 an arbitrary number of arguments is supported. 432 </p> 433</div> 434<div class="section"> 435<div class="titlepage"><div><div><h3 class="title"> 436<a name="boost_functional_factory.reference.factory"></a><a class="link" href="index.html#boost_functional_factory.reference.factory" title="factory">factory</a> 437</h3></div></div></div> 438<h5> 439<a name="boost_functional_factory.reference.factory.h0"></a> 440 <span class="phrase"><a name="boost_functional_factory.reference.factory.description"></a></span><a class="link" href="index.html#boost_functional_factory.reference.factory.description">Description</a> 441 </h5> 442<p> 443 Function object template that dynamically constructs a pointee object for 444 the type of pointer given as template argument. Smart pointers may be used 445 for the template argument, given that <code class="computeroutput"><span class="identifier">pointer_traits</span><span class="special"><</span><span class="identifier">Pointer</span><span class="special">>::</span><span class="identifier">element_type</span></code> 446 yields the pointee type. 447 </p> 448<p> 449 If an <a href="https://www.boost.org/sgi/stl/Allocators.html" target="_top">Allocator</a> 450 is given, it is used for memory allocation and the placement form of the 451 <code class="computeroutput"><span class="keyword">new</span></code> operator is used to construct 452 the object. A function object that calls the destructor and deallocates the 453 memory with a copy of the Allocator is used for the second constructor argument 454 of <code class="computeroutput"><span class="identifier">Pointer</span></code> (thus it must 455 be a <a href="http://www.boost.org/libs/smart_ptr/index.html" target="_top">Smart Pointer</a> 456 that provides a suitable constructor, such as <a href="http://www.boost.org/libs/smart_ptr/shared_ptr.htm" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span></code></a>). 457 </p> 458<p> 459 If a third template argument is <code class="computeroutput"><span class="identifier">factory_passes_alloc_to_smart_pointer</span></code>, 460 the allocator itself is used for the third constructor argument of <code class="computeroutput"><span class="identifier">Pointer</span></code> ( <a href="http://www.boost.org/libs/smart_ptr/shared_ptr.htm" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span></code></a> then uses the allocator 461 to manage the memory of its separately allocated reference counter). 462 </p> 463<h5> 464<a name="boost_functional_factory.reference.factory.h1"></a> 465 <span class="phrase"><a name="boost_functional_factory.reference.factory.header"></a></span><a class="link" href="index.html#boost_functional_factory.reference.factory.header">Header</a> 466 </h5> 467<p> 468</p> 469<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">functional</span><span class="special">/</span><span class="identifier">factory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 470</pre> 471<p> 472 </p> 473<h5> 474<a name="boost_functional_factory.reference.factory.h2"></a> 475 <span class="phrase"><a name="boost_functional_factory.reference.factory.synopsis"></a></span><a class="link" href="index.html#boost_functional_factory.reference.factory.synopsis">Synopsis</a> 476 </h5> 477<p> 478</p> 479<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> 480 481<span class="keyword">enum</span> <span class="identifier">factory_alloc_propagation</span> <span class="special">{</span> 482 <span class="identifier">factory_alloc_for_pointee_and_deleter</span><span class="special">,</span> 483 <span class="identifier">factory_passes_alloc_to_smart_pointer</span> 484<span class="special">};</span> 485 486<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Pointer</span><span class="special">,</span> 487 <span class="keyword">class</span> <span class="identifier">Allocator</span> <span class="special">=</span> <span class="keyword">void</span><span class="special">,</span> 488 <span class="identifier">factory_alloc_propagation</span> <span class="identifier">Policy</span> <span class="special">=</span> <span class="identifier">factory_alloc_for_pointee_and_deleter</span><span class="special">></span> 489<span class="keyword">class</span> <span class="identifier">factory</span><span class="special">;</span> 490 491<span class="special">}</span> <span class="comment">// boost</span> 492</pre> 493<p> 494 </p> 495<div class="variablelist"> 496<p class="title"><b>Notation</b></p> 497<dl class="variablelist"> 498<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span></code></span></dt> 499<dd><p> 500 an arbitrary type with at least one public constructor 501 </p></dd> 502<dt><span class="term"><code class="computeroutput"><span class="identifier">P</span></code></span></dt> 503<dd><p> 504 pointer or smart pointer to <code class="computeroutput"><span class="identifier">T</span></code> 505 </p></dd> 506<dt><span class="term"><code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code></span></dt> 507<dd><p> 508 argument values to a constructor of <code class="computeroutput"><span class="identifier">T</span></code> 509 </p></dd> 510<dt><span class="term"><code class="computeroutput"><span class="identifier">F</span></code></span></dt> 511<dd><p> 512 the type <code class="computeroutput"><span class="identifier">factory</span><span class="special"><</span><span class="identifier">P</span><span class="special">></span></code> 513 </p></dd> 514<dt><span class="term"><code class="computeroutput"><span class="identifier">f</span></code></span></dt> 515<dd><p> 516 an instance object of <code class="computeroutput"><span class="identifier">F</span></code> 517 </p></dd> 518</dl> 519</div> 520<h5> 521<a name="boost_functional_factory.reference.factory.h3"></a> 522 <span class="phrase"><a name="boost_functional_factory.reference.factory.expression_semantics"></a></span><a class="link" href="index.html#boost_functional_factory.reference.factory.expression_semantics">Expression 523 Semantics</a> 524 </h5> 525<div class="informaltable"><table class="table"> 526<colgroup> 527<col> 528<col> 529</colgroup> 530<thead><tr> 531<th> 532 <p> 533 Expression 534 </p> 535 </th> 536<th> 537 <p> 538 Semantics 539 </p> 540 </th> 541</tr></thead> 542<tbody> 543<tr> 544<td> 545 <p> 546 <code class="computeroutput"><span class="identifier">F</span><span class="special">()</span></code> 547 </p> 548 </td> 549<td> 550 <p> 551 creates an object of type <code class="computeroutput"><span class="identifier">F</span></code>. 552 </p> 553 </td> 554</tr> 555<tr> 556<td> 557 <p> 558 <code class="computeroutput"><span class="identifier">F</span><span class="special">(</span><span class="identifier">f</span><span class="special">)</span></code> 559 </p> 560 </td> 561<td> 562 <p> 563 creates an object of type <code class="computeroutput"><span class="identifier">F</span></code>. 564 </p> 565 </td> 566</tr> 567<tr> 568<td> 569 <p> 570 <code class="computeroutput"><span class="identifier">f</span><span class="special">(</span><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span><span class="special">)</span></code> 571 </p> 572 </td> 573<td> 574 <p> 575 dynamically creates an object of type <code class="computeroutput"><span class="identifier">T</span></code> 576 using <code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code> as arguments for the constructor 577 invocation. 578 </p> 579 </td> 580</tr> 581<tr> 582<td> 583 <p> 584 <code class="computeroutput"><span class="identifier">F</span><span class="special">::</span><span class="identifier">result_type</span></code> 585 </p> 586 </td> 587<td> 588 <p> 589 is the type <code class="computeroutput"><span class="identifier">P</span></code> with 590 top-level cv-qualifiers removed. 591 </p> 592 </td> 593</tr> 594</tbody> 595</table></div> 596<h5> 597<a name="boost_functional_factory.reference.factory.h4"></a> 598 <span class="phrase"><a name="boost_functional_factory.reference.factory.limits"></a></span><a class="link" href="index.html#boost_functional_factory.reference.factory.limits">Limits</a> 599 </h5> 600<p> 601 Before C++11, the maximum number of arguments supported is 10. Since C++11 602 an arbitrary number of arguments is supported. 603 </p> 604</div> 605</div> 606<div class="section"> 607<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 608<a name="boost_functional_factory.changes"></a><a class="link" href="index.html#boost_functional_factory.changes" title="Changes">Changes</a> 609</h2></div></div></div> 610<h4> 611<a name="boost_functional_factory.changes.h0"></a> 612 <span class="phrase"><a name="boost_functional_factory.changes.boost_1_72_0"></a></span><a class="link" href="index.html#boost_functional_factory.changes.boost_1_72_0">Boost 613 1.72.0</a> 614 </h4> 615<p> 616 Glen Fernandes rewrote the implementations of <code class="computeroutput"><span class="identifier">factory</span></code> 617 and <code class="computeroutput"><span class="identifier">value_factory</span></code> to provide 618 the following features: 619 </p> 620<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 621<li class="listitem"> 622 Support r-value arguments when available 623 </li> 624<li class="listitem"> 625 Support arbitrary number of arguments via variadic templates when available 626 </li> 627<li class="listitem"> 628 Support allocators that are final 629 </li> 630<li class="listitem"> 631 Support allocators that use fancy pointers 632 </li> 633<li class="listitem"> 634 Support for disabled exceptions (<code class="computeroutput"><span class="identifier">BOOST_NO_EXCEPTIONS</span></code>) 635 </li> 636<li class="listitem"> 637 Improved compilation times 638 </li> 639</ul></div> 640<p> 641 The following features have been removed: 642 </p> 643<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 644<li class="listitem"> 645 Increasing limits for C++03 compilers through <code class="computeroutput"><span class="identifier">BOOST_FUNCTIONAL_VALUE_FACTORY_MAX_ARITY</span></code> 646 </li> 647<li class="listitem"> 648 Using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">none_t</span></code> in place of <code class="computeroutput"><span class="keyword">void</span></code> 649 through <code class="computeroutput"><span class="identifier">BOOST_FUNCTIONAL_FACTORY_SUPPORT_NONE_T</span></code> 650 </li> 651</ul></div> 652<h4> 653<a name="boost_functional_factory.changes.h1"></a> 654 <span class="phrase"><a name="boost_functional_factory.changes.boost_1_58_0"></a></span><a class="link" href="index.html#boost_functional_factory.changes.boost_1_58_0">Boost 655 1.58.0</a> 656 </h4> 657<p> 658 In order to remove the dependency on Boost.Optional, the default parameter 659 for allocators has been changed from <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">none_t</span></code> 660 to <code class="computeroutput"><span class="keyword">void</span></code>. If you have code that 661 has stopped working because it uses <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">none_t</span></code>, 662 a quick fix is to define <code class="computeroutput"><span class="identifier">BOOST_FUNCTIONAL_FACTORY_SUPPORT_NONE_T</span></code>, 663 which will restore support, but this will be removed in a future release. It 664 should be be relatively easy to fix this properly. 665 </p> 666</div> 667<div class="section"> 668<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 669<a name="boost_functional_factory.acknowledgements"></a><a class="link" href="index.html#boost_functional_factory.acknowledgements" title="Acknowledgements">Acknowledgements</a> 670</h2></div></div></div> 671<p> 672 Tobias Schwinger for creating this library. 673 </p> 674<p> 675 Eric Niebler requested a function to invoke a type's constructor (with the 676 arguments supplied as a Tuple) as a Fusion feature. These Factory utilities 677 are a factored-out generalization of this idea. 678 </p> 679<p> 680 Dave Abrahams suggested Smart Pointer support for exception safety, providing 681 useful hints for the implementation. 682 </p> 683<p> 684 Joel de Guzman's documentation style was copied from Fusion. 685 </p> 686<p> 687 Peter Dimov for sharing his insights on language details and their evolution. 688 </p> 689</div> 690<div class="section"> 691<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 692<a name="boost_functional_factory.references"></a><a class="link" href="index.html#boost_functional_factory.references" title="References">References</a> 693</h2></div></div></div> 694<div class="orderedlist"><ol class="orderedlist" type="1"> 695<li class="listitem"> 696 <a href="http://en.wikipedia.org/wiki/Design_Patterns" target="_top">Design Patterns</a>, 697 Gamma et al. - Addison Wesley Publishing, 1995 698 </li> 699<li class="listitem"> 700 <a href="https://boost.org/sgi/stl/" target="_top">Standard Template Library Programmer's 701 Guide</a>, Hewlett-Packard Company, 1994 702 </li> 703<li class="listitem"> 704 <a href="http://www.boost.org/libs/bind/bind.html" target="_top">Boost.Bind</a>, 705 Peter Dimov, 2001-2005 706 </li> 707<li class="listitem"> 708 <a href="http://www.boost.org/doc/html/function.html" target="_top">Boost.Function</a>, 709 Douglas Gregor, 2001-2004 710 </li> 711</ol></div> 712</div> 713</div> 714<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 715<td align="left"><p><small>Last revised: August 11, 2020 at 15:01:09 GMT</small></p></td> 716<td align="right"><div class="copyright-footer"></div></td> 717</tr></table> 718<hr> 719<div class="spirit-nav"></div> 720</body> 721</html> 722