Unlike MPL, Fusion algorithms
are lazy and non sequence-type preserving . This is by design. Runtime efficiency is given a high priority.
Like MPL, and unlike STL, fusion
algorithms are mostly 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
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
push_back to be totally generic. In
push_back is actually a generic algorithm
that works on all sequences. Given an input sequence
and a value
push_back algorithm simply returns
a view that holds a reference to the original sequence
and the value
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.
#include <boost/fusion/algorithm.hpp> #include <boost/fusion/include/algorithm.hpp>