1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Introduction</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. Fusion 2.2"> 8<link rel="up" href="../index.html" title="Chapter 1. Fusion 2.2"> 9<link rel="prev" href="preface.html" title="Preface"> 10<link rel="next" href="quick_start.html" title="Quick Start"> 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="preface.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="quick_start.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="fusion.introduction"></a><a class="link" href="introduction.html" title="Introduction">Introduction</a> 28</h2></div></div></div> 29<p> 30 An advantage other languages such as Python and Lisp/ Scheme, ML and Haskell, 31 etc., over C++ is the ability to have heterogeneous containers that can hold 32 arbitrary element types. All the containers in the standard library can only 33 hold a specific type. A <code class="computeroutput"><span class="identifier">vector</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span></code> 34 can only hold <code class="computeroutput"><span class="keyword">int</span></code>s. A <code class="computeroutput"><span class="identifier">list</span><span class="special"><</span><span class="identifier">X</span><span class="special">></span></code> can 35 only hold elements of type <code class="computeroutput"><span class="identifier">X</span></code>, 36 and so on. 37 </p> 38<p> 39 True, you can use inheritance to make the containers hold different types, 40 related through subclassing. However, you have to hold the objects through 41 a pointer or smart reference of some sort. Doing this, you'll have to rely 42 on virtual functions to provide polymorphic behavior since the actual type 43 is erased as soon as you store a pointer to a derived class to a pointer to 44 its base. The held objects must be related: you cannot hold objects of unrelated 45 types such as <code class="computeroutput"><span class="keyword">char</span></code>, <code class="computeroutput"><span class="keyword">int</span></code>, <code class="computeroutput"><span class="keyword">class</span> 46 <span class="identifier">X</span></code>, <code class="computeroutput"><span class="keyword">float</span></code>, 47 etc. Oh sure you can use something like <a href="http://www.boost.org/libs/any" target="_top">Boost.Any 48 Library</a> to hold arbitrary types, but then you pay more in terms of 49 runtime costs and due to the fact that you practically erased all type information, 50 you'll have to perform dangerous casts to get back the original type. 51 </p> 52<p> 53 The <a href="http://www.boost.org/libs/tuple" target="_top">Boost.Tuple</a> library 54 written by <a href="http://www.boost.org/people/jaakko_jarvi.htm" target="_top">Jaakko 55 Jarvi</a> provides heterogeneous containers in C++. The <code class="computeroutput"><span class="identifier">tuple</span></code> 56 is a basic data structure that can hold heterogeneous types. It's a good first 57 step, but it's not complete. What's missing are the algorithms. It's nice that 58 we can store and retrieve data to and from tuples, pass them around as arguments 59 and return types. As it is, the <a href="http://www.boost.org/libs/tuple" target="_top">Boost.Tuple</a> 60 facility is already very useful. Yet, as soon as you use it more often, usage 61 patterns emerge. Eventually, you collect these patterns into algorithm libraries. 62 </p> 63<p> 64 Hmmm, kinda reminds us of STL right? Right! Can you imagine how it would be 65 like if you used STL without the algorithms? Everyone will have to reinvent 66 their own <span class="emphasis"><em>algorithm</em></span> wheels. 67 </p> 68<p> 69 Fusion is a library and a framework similar to both <a href="http://en.wikipedia.org/wiki/Standard_Template_Library" target="_top">STL</a> 70 and the boost <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a>. The structure 71 is modeled after <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a>, which 72 is modeled after <a href="http://en.wikipedia.org/wiki/Standard_Template_Library" target="_top">STL</a>. 73 It is named "fusion" because the library is reminiscent of the "fusion" 74 of compile time meta-programming with runtime programming. The library inherently 75 has some interesting flavors and characteristics of both <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> 76 and <a href="http://en.wikipedia.org/wiki/Standard_Template_Library" target="_top">STL</a>. 77 It lives in the twilight zone between compile time meta-programming and run 78 time programming. <a href="http://en.wikipedia.org/wiki/Standard_Template_Library" target="_top">STL</a> 79 containers work on values. MPL containers work on types. Fusion containers 80 work on both types and values. 81 </p> 82<p> 83 Unlike <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a>, Fusion algorithms 84 are lazy and non sequence-type preserving. What does that mean? It means that 85 when you operate on a sequence through a Fusion algorithm that returns a sequence, 86 the sequence returned may not be of the same class as the original. This is 87 by design. Runtime efficiency is given a high priority. Like <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a>, 88 and unlike <a href="http://en.wikipedia.org/wiki/Standard_Template_Library" target="_top">STL</a>, 89 fusion algorithms are functional in nature such that algorithms are non mutating 90 (no side effects). However, due to the high cost of returning full sequences 91 such as vectors and lists, <span class="emphasis"><em>Views</em></span> are returned from Fusion 92 algorithms instead. For example, the <a class="link" href="algorithm/transformation/functions/transform.html" title="transform"><code class="computeroutput"><span class="identifier">transform</span></code></a> algorithm does not actually 93 return a transformed version of the original sequence. <a class="link" href="algorithm/transformation/functions/transform.html" title="transform"><code class="computeroutput"><span class="identifier">transform</span></code></a> returns a <a class="link" href="view/transform_view.html" title="transform_view"><code class="computeroutput"><span class="identifier">transform_view</span></code></a>. This view holds a 94 reference to the original sequence plus the transform function. Iteration over 95 the <a class="link" href="view/transform_view.html" title="transform_view"><code class="computeroutput"><span class="identifier">transform_view</span></code></a> 96 will apply the transform function over the sequence elements on demand. This 97 <span class="emphasis"><em>lazy</em></span> evaluation scheme allows us to chain as many algorithms 98 as we want without incurring a high runtime penalty. 99 </p> 100<p> 101 The <span class="emphasis"><em>lazy</em></span> evaluation scheme where algorithms return views 102 allows operations such as <a class="link" href="algorithm/transformation/functions/push_back.html" title="push_back"><code class="computeroutput"><span class="identifier">push_back</span></code></a> to be totally generic. In 103 Fusion, <a class="link" href="algorithm/transformation/functions/push_back.html" title="push_back"><code class="computeroutput"><span class="identifier">push_back</span></code></a> is actually a generic algorithm 104 that works on all sequences. Given an input sequence <code class="computeroutput"><span class="identifier">s</span></code> 105 and a value <code class="computeroutput"><span class="identifier">x</span></code>, Fusion's <a class="link" href="algorithm/transformation/functions/push_back.html" title="push_back"><code class="computeroutput"><span class="identifier">push_back</span></code></a> algorithm simply returns 106 a <a class="link" href="view/joint_view.html" title="joint_view"><code class="computeroutput"><span class="identifier">joint_view</span></code></a>: 107 a view that holds a reference to the original sequence <code class="computeroutput"><span class="identifier">s</span></code> 108 and the value <code class="computeroutput"><span class="identifier">x</span></code>. Functions 109 that were once sequence specific and need to be implemented N times over N 110 different sequences are now implemented only once. 111 </p> 112<p> 113 Fusion provides full round compatibility with <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a>. 114 Fusion sequences are fully conforming <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> 115 sequences and <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> sequences 116 are fully compatible with Fusion. You can work with Fusion sequences on <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> if you wish to work solely 117 on types <a href="#ftn.fusion.introduction.f0" class="footnote" name="fusion.introduction.f0"><sup class="footnote">[1]</sup></a>. In <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a>, Fusion 118 sequences follow <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a>'s sequence-type 119 preserving semantics (i.e. algorithms preserve the original sequence type. 120 e.g. transforming a vector returns a vector). You can also convert from an 121 <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> sequence to a Fusion 122 sequence. For example, there are times when it is convenient to work solely 123 on <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> using pure <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> sequences, then, convert them 124 to Fusion sequences as a final step before actual instantiation of real runtime 125 objects with data. You have the best of both worlds. 126 </p> 127<div class="footnotes"> 128<br><hr style="width:100; text-align:left;margin-left: 0"> 129<div id="ftn.fusion.introduction.f0" class="footnote"><p><a href="#fusion.introduction.f0" class="para"><sup class="para">[1] </sup></a> 130 Choose <a href="http://www.boost.org/libs/mpl" target="_top">MPL</a> over fusion 131 when doing pure type calculations. Once the static type calculation is finished, 132 you can instantiate a fusion sequence (see <a class="link" href="container/conversion/functions.html" title="Functions">Conversion</a>) 133 for the runtime part. 134 </p></div> 135</div> 136</div> 137<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 138<td align="left"></td> 139<td align="right"><div class="copyright-footer">Copyright © 2001-2006, 2011, 2012 Joel de Guzman, 140 Dan Marsden, Tobias Schwinger<p> 141 Distributed under the Boost Software License, Version 1.0. (See accompanying 142 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>) 143 </p> 144</div></td> 145</tr></table> 146<hr> 147<div class="spirit-nav"> 148<a accesskey="p" href="preface.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="quick_start.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 149</div> 150</body> 151</html> 152