introduction.qbk 5.4 KB

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