...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The Plus generator is used to repeat the execution of an embedded 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/plus.hpp> #include <boost/spirit/include/karma_plus.hpp>
Also, see Include Structure.
Semantics of an expression is defined only where it differs from, or
is not defined in UnaryGenerator
.
Expression |
Semantics |
---|---|
|
The generator |
Note | |
---|---|
All failing iterations of the embedded generator will consume one element
from the supplied attribute. The overall |
See Compound Attribute Notation.
Expression |
Attribute |
---|---|
|
a: A --> +a: vector<A> a: Unused --> +a: Unused
|
Important | |
---|---|
The table above uses |
The Plus generator will execute its embedded generator once for each element in the provided container attribute as long as the embedded generator succeeds. On each iteration it will pass the next consecutive element from the container attribute to the embedded generator. 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 plus generator fail.
It is important to note, that the plus generator does not perform any buffering of the output generated by its embedded elements. That means that any failing element generator might have already generated some output, which is not rolled back.
Tip | |
---|---|
The simplest way to force a plus generator to behave as if it did buffering
is to wrap it into a buffering directive (see
buffer[+a]
which will not generate any output in case of
a failing generator
+(buffer[a])
will not generate any partial output from a generator |
The overall complexity of the plus generator is defined by the complexity of its embedded generator multiplied by the number of executed iterations. The complexity of the plus generator itself is O(N), where N is the number of elements in the container passed as its attribute.
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
Some includes:
#include <boost/spirit/include/karma.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_; using boost::spirit::karma::space;
Basic usage of a plus generator:
std::vector<double> v1; v1.push_back(1.0); v1.push_back(2.0); v1.push_back(3.0); test_generator_attr_delim("1.0 2.0 3.0 ", +double_, space, v1);
A more sophisticated use case showing how to leverage the fact that plus is failing for empty containers passed as its attribute:
std::vector<double> v2; // empty container test_generator_attr("empty", +double_ | "empty", v2);