Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext
Consume Attribute (omit[])
Description

Consumes the attribute type of the embedded generator without generating any output.

Header
// forwards to <boost/spirit/home/karma/directive/omit.hpp>
#include <boost/spirit/include/karma_omit.hpp>

Also, see Include Structure.

Name

boost::spirit::omit // alias: boost::spirit::karma::omit

Model of

UnaryGenerator

Notation

a

A generator object

A

Attribute type of generator a

Expression Semantics

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

Expression

Semantics

omit[a]

The omit directive consumes the attribute type of the embedded generator A without generating any output. It succeeds always.

Attributes

See Compound Attribute Notation.

Expression

Attribute

omit[a]

a: A --> omit[a]: A
a: Unused --> omit[a]: Unused

Complexity

The overall complexity of the omit generator directive is O(1) as it does not generate any output.

Example
[Note] 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::omit;

Basic usage of a omit generator directive. It shows how it consumes the first element of the provided attribute without generating anything, leaving the second element of the attribute to the non-wrapped double_ generator.

std::pair<double, double> p (1.0, 2.0);
test_generator_attr("2.0", omit[double_] << double_, p);

Generally, this directive is helpful in situations, where the attribute type contains more information (elements) than need to be used to generate the required output. Normally in such situations we would resolve to use semantic actions to explicitly pass the correct parts of the overall attribute to the generators. The omit directive helps achieving the same without having to use semantic actions.

Consider the attribute type:

typedef fusion::vector<int, double, std::string> attribute_type;

where we need to generate output only from the first and last element:

typedef std::back_insert:iterator<std::string> iterator_type;

karma::rule<iterator_type, attribute_type()> r;
r = int_[_1 = phoenix::at_c<0>(_val)] << string[_1 = phoenix::at_c<2>(_val)];

std::string str;
iterator_type sink(str);
generate(sink, r, attribute_type(1, 2.0, "example"));  // will generate: '1example'

This is error prone and not really readable. The same can be achieved by using the omit directive:

r = int_ << omit[double_] << string;

which is at the same time more readable and more efficient as we don't have to use semantic actions.


PrevUpHomeNext