📄 introduction.qbk
字号:
[/============================================================================== Copyright (C) 2001-2007 Joel de Guzman, Dan Marsden, Tobias Schwinger Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)===============================================================================/][section Introduction]An advantage other languages such as Python and Lisp/ Scheme, ML andHaskell, etc., over C++ is the ability to have heterogeneous containersthat can hold arbitrary element types. All the containers in the standardlibrary can only hold a specific type. A `vector<int>` can only hold`int`s. A `list<X>` can only hold elements of type `X`, and so on.True, you can use inheritance to make the containers hold different types,related through subclassing. However, you have to hold the objects througha pointer or smart reference of some sort. Doing this, you'll have to relyon virtual functions to provide polymorphic behavior since the actual typeis erased as soon as you store a pointer to a derived class to a pointer toits base. The held objects must be related: you cannot hold objects ofunrelated types such as `char`, `int`, `class X`, `float`, etc. Oh sure youcan use something like __boost_any__ to hold arbitrary types, but then youpay more in terms of runtime costs and due to the fact that you practicallyerased all type information, you'll have to perform dangerous casts to getback the original type.The __tuple__ library written by __jaakko_jarvi__ provides heterogeneouscontainers in C++. The `tuple` is a basic data structure that can holdheterogeneous types. It's a good first step, but it's not complete. What'smissing are the algorithms. It's nice that we can store and retrieve datato and from tuples, pass them around as arguments and return types. As itis, the __tuple__ facility is already very useful. Yet, as soon as you useit more often, usage patterns emerge. Eventually, you collect thesepatterns into algorithm libraries.Hmmm, kinda reminds us of STL right? Right! Can you imagine how it would belike if you used STL without the algorithms? Everyone will have to reinventtheir own /algorithm/ wheels.Fusion is a library and a framework similar to both __stl__ and the boost__mpl__. The structure is modeled after __mpl__, which is modeledafter __stl__. It is named "fusion" because the library is reminiscent ofthe "fusion" of compile time meta-programming with runtime programming. Thelibrary inherently has some interesting flavors and characteristics of both__mpl__ and __stl__. It lives in the twilight zone between compile timemeta-programming and run time programming. __stl__ containers work onvalues. MPL containers work on types. Fusion containers work on both typesand values.Unlike __mpl__, Fusion algorithms are lazy and non sequence-typepreserving. What does that mean? It means that when you operate on asequence through a Fusion algorithm that returns a sequence, the sequencereturned may not be of the same class as the original. This is by design.Runtime efficiency is given a high priority. Like __mpl__, and unlike__stl__, fusion algorithms are functional in nature such that algorithmsare non mutating (no side effects). However, due to the high cost ofreturning full sequences such as vectors and lists, /Views/ are returnedfrom Fusion algorithms instead. For example, the __transform__ algorithmdoes not actually return a transformed version of the original sequence.__transform__ returns a __transform_view__. This view holds a reference tothe original sequence plus the transform function. Iteration over the__transform_view__ will apply the transform function over the sequenceelements on demand. This /lazy/ evaluation scheme allows us to chain asmany algorithms as we want without incurring a high runtime penalty.The /lazy/ evaluation scheme where algorithms return views allowsoperations such as __push_back__ to be totally generic. In Fusion,__push_back__ is actually a generic algorithm that works on all sequences.Given an input sequence `s` and a value `x`, Fusion's __push_back__algorithm simply returns a __joint_view__: a view that holds a reference tothe original sequence `s` and the value `x`. Functions that were oncesequence specific and need to be implemented N times over N differentsequences are now implemented only once.Fusion provides full round compatibility with __mpl__. Fusion sequences arefully conforming __mpl__ sequences and __mpl__ sequences are fully compatiblewith Fusion. You can work with Fusion sequences on __mpl__ if you wish to worksolely on types [footnote Choose __mpl__ over fusion when doing pure typecalculations. Once the static type calculation is finished, you can instantiatea fusion sequence (see __conversion__) for the runtime part.]. In __mpl__,Fusion sequences follow __mpl__'s sequence-type preserving semantics (i.e.algorithms preserve the original sequence type. e.g. transforming a vectorreturns a vector). You can also convert from an __mpl__ sequence to a Fusionsequence. For example, there are times when it is convenient to work solely on__mpl__ using pure __mpl__ sequences, then, convert them to Fusion sequences asa final step before actual instantiation of real runtime objects with data. Youhave the best of both worlds.[endsect]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -