1[/============================================================================== 2 Copyright (C) 2001-2011 Joel de Guzman 3 Copyright (C) 2006 Dan Marsden 4 5 Use, modification and distribution is subject to the Boost Software 6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 7 http://www.boost.org/LICENSE_1_0.txt) 8===============================================================================/] 9[section Introduction] 10 11An advantage other languages such as Python and Lisp/ Scheme, ML and 12Haskell, etc., over C++ is the ability to have heterogeneous containers 13that can hold arbitrary element types. All the containers in the standard 14library can only hold a specific type. A `vector<int>` can only hold 15`int`s. A `list<X>` can only hold elements of type `X`, and so on. 16 17True, you can use inheritance to make the containers hold different types, 18related through subclassing. However, you have to hold the objects through 19a pointer or smart reference of some sort. Doing this, you'll have to rely 20on virtual functions to provide polymorphic behavior since the actual type 21is erased as soon as you store a pointer to a derived class to a pointer to 22its base. The held objects must be related: you cannot hold objects of 23unrelated types such as `char`, `int`, `class X`, `float`, etc. Oh sure you 24can use something like __boost_any__ to hold arbitrary types, but then you 25pay more in terms of runtime costs and due to the fact that you practically 26erased all type information, you'll have to perform dangerous casts to get 27back the original type. 28 29The __tuple__ library written by __jaakko_jarvi__ provides heterogeneous 30containers in C++. The `tuple` is a basic data structure that can hold 31heterogeneous types. It's a good first step, but it's not complete. What's 32missing are the algorithms. It's nice that we can store and retrieve data 33to and from tuples, pass them around as arguments and return types. As it 34is, the __tuple__ facility is already very useful. Yet, as soon as you use 35it more often, usage patterns emerge. Eventually, you collect these 36patterns into algorithm libraries. 37 38Hmmm, kinda reminds us of STL right? Right! Can you imagine how it would be 39like if you used STL without the algorithms? Everyone will have to reinvent 40their own /algorithm/ wheels. 41 42Fusion is a library and a framework similar to both __stl__ and the boost 43__mpl__. The structure is modeled after __mpl__, which is modeled 44after __stl__. It is named "fusion" because the library is reminiscent of 45the "fusion" of compile time meta-programming with runtime programming. The 46library inherently has some interesting flavors and characteristics of both 47__mpl__ and __stl__. It lives in the twilight zone between compile time 48meta-programming and run time programming. __stl__ containers work on 49values. MPL containers work on types. Fusion containers work on both types 50and values. 51 52Unlike __mpl__, Fusion algorithms are lazy and non sequence-type 53preserving. What does that mean? It means that when you operate on a 54sequence through a Fusion algorithm that returns a sequence, the sequence 55returned may not be of the same class as the original. This is by design. 56Runtime efficiency is given a high priority. Like __mpl__, and unlike 57__stl__, fusion algorithms are functional in nature such that algorithms 58are non mutating (no side effects). However, due to the high cost of 59returning full sequences such as vectors and lists, /Views/ are returned 60from Fusion algorithms instead. For example, the __transform__ algorithm 61does not actually return a transformed version of the original sequence. 62__transform__ returns a __transform_view__. This view holds a reference to 63the original sequence plus the transform function. Iteration over the 64__transform_view__ will apply the transform function over the sequence 65elements on demand. This /lazy/ evaluation scheme allows us to chain as 66many algorithms as we want without incurring a high runtime penalty. 67 68The /lazy/ evaluation scheme where algorithms return views allows 69operations such as __push_back__ to be totally generic. In Fusion, 70__push_back__ is actually a generic algorithm that works on all sequences. 71Given an input sequence `s` and a value `x`, Fusion's __push_back__ 72algorithm simply returns a __joint_view__: a view that holds a reference to 73the original sequence `s` and the value `x`. Functions that were once 74sequence specific and need to be implemented N times over N different 75sequences are now implemented only once. 76 77Fusion provides full round compatibility with __mpl__. Fusion sequences are 78fully conforming __mpl__ sequences and __mpl__ sequences are fully compatible 79with Fusion. You can work with Fusion sequences on __mpl__ if you wish to work 80solely on types [footnote Choose __mpl__ over fusion when doing pure type 81calculations. Once the static type calculation is finished, you can instantiate 82a fusion sequence (see __conversion__) for the runtime part.]. In __mpl__, 83Fusion sequences follow __mpl__'s sequence-type preserving semantics (i.e. 84algorithms preserve the original sequence type. e.g. transforming a vector 85returns a vector). You can also convert from an __mpl__ sequence to a Fusion 86sequence. For example, there are times when it is convenient to work solely on 87__mpl__ using pure __mpl__ sequences, then, convert them to Fusion sequences as 88a final step before actual instantiation of real runtime objects with data. You 89have the best of both worlds. 90 91[endsect] 92