

Click here to view the latest version of this page.
List Generator (a % b)

The list generator is used to repeat the execution of an embedded generator and intersperse it with the output of another generator one or more times. It succeeds if the embedded generator has been successfully executed at least once.

// forwards to <boost/spirit/home/karma/operator/list.hpp>
#include <boost/spirit/include/karma_list.hpp>

Also, see Include Structure.

Model of


Expression Semantics

Semantics of an expression is defined only where it differs from, or is not defined in BinaryGenerator.



a % b

The generator a is executed one or more times depending on the availability of an attribute. The output generated by a is interspersed with the output generated by b. The list generator succeeds if its first embedded generator has been successfully executed at least once (unless the underlying output stream reports an error).

The list expression a % b is a shortcut for a << *(b << a). It is almost semantically equivalent, except for the attribute of b, which gets ignored in the case of the list generator.

[Note] Note

All failing iterations of the embedded generator will consume one element from the supplied attribute. The overall a % b will succeed as long as at least one invocation of the embedded generator, a, will succeed (unless the underlying output stream reports an error).


See Compound Attribute Notation.



a % b (list)

a: A, b: B --> (a % b): vector<A>
a: Unused, b: B --> (a % b): Unused

[Important] Important


The list generator will execute its embedded generator once for each element in the provided container attribute and as long as the embedded generator succeeds. The output generated by its first generator will be interspersed by the output generated by the second generator. On each iteration it will pass the next consecutive element from the container attribute to the first embedded generator. The second embedded generator does not get passed any attributes (it gets invoked using an unused_type as its attribute). Therefore the number of iterations will not be larger than the number of elements in the container passed as its attribute. An empty container will make the list generator fail.

[Tip] Tip

If you want to use the list generator and still allow for an empty attribute, you can use the optional operator (see Optional (unary -)):

-(a % b)

which will succeed even if the provided container attribute does not contain any elements.


The overall complexity of the list generator is defined by the complexity of its embedded generators multiplied by the number of executed iterations. The complexity of the list generator itself is O(N), where N is the number of elements in the container passed as its attribute.

[Note] Note


Some includes:

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <iostream>
#include <string>

Some using declarations:

using boost::spirit::karma::double_;

Basic usage of a list generator:

std::vector<double> v1;
test_generator_attr("1.0", double_ % ',', v1);

test_generator_attr("1.0,2.0", double_ % ',', v1);