...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Unlike MPL, Fusion
algorithms are lazy and non sequence-type preserving. What does that mean?
It means that when you operate on a sequence through a Fusion algorithm that
returns a sequence, the sequence returned 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 algorithms are non mutating
(no side effects). However, due to the high cost of returning full sequences
such as vectors and lists, Views are returned from Fusion
algorithms instead. For example, the transform
algorithm does not actually
return a transformed version of the original sequence. transform
returns a transform_view
. This view holds a
reference to the original sequence plus the transform function. Iteration over
the transform_view
will apply the transform function over the sequence elements on demand. This
lazy evaluation scheme allows us to chain as many algorithms
as we want without incurring a high runtime penalty.
The lazy evaluation scheme where Algorithms
return Views also allows operations 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 to the original sequence s
and the value x
. Functions
that were once sequence specific and need to be implemented N times over N
different sequences are now implemented only once. That is to say that Fusion
sequences are cheaply extensible. However, an important caveat is that the
result of a sequence extending operation like push_back
does not retain the properties
of the original sequence such as associativity of set
(s). To regain the original sequence,
Conversion functions
are provided. You may use one of the Conversion
functions to convert back to the original sequence type.
#include <boost/fusion/algorithm.hpp> #include <boost/fusion/include/algorithm.hpp>