| Home | Libraries | People | FAQ | More

##### Plus (`+a`)

###### Description

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.

###### Header

    // forwards to <boost/spirit/home/karma/operator/plus.hpp>
    #include <boost/spirit/include/karma_plus.hpp>

Also, see Include Structure.

###### Model of

> `UnaryGenerator`

###### Expression Semantics

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

Expression | Semantics
--- | ---
`+a` | The generator `a` is executed one or more times depending on the availability of an attribute. The execution of `a` stops after the attribute values passed to the plus generator are exhausted. The plus generator succeeds as long as its embedded generator has been successfully executed at least once (unless the underlying output stream reports an error).

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

###### Attributes

See Compound Attribute Notation.

Expression | Attribute
--- | ---

`+a` (unary `+`)

    a: A --> +a: vector<A>
    a: Unused --> +a: Unused

| Important
The table above uses `vector<A>` as a placeholder only. The notation of `vector<A>` stands for any STL container holding elements of type `A`.

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`):

    buffer[+a]

which will not generate any output in case of a failing generator `+a`. The expression:

    +(buffer[a])

will not generate any partial output from a generator `a` if it fails generating in the middle of its output. The overall expression will still generate the output as produced by all successful invocations of the generator `a`.

###### Complexity

> 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.

###### Example

| 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);