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
Signed Integer Number Generators (int_, etc.)
Description

The int_generator can generate signed integers of arbitrary length and size. This is almost the same as the uint_generator. The only difference is the additional task of generating the '+' or '-' sign preceding the number. The class interface is the same as that of the uint_generator.

The int_generator generator can be used to emit ordinary primitive C/C++ integers or even user defined scalars such as bigints (unlimited precision integers) if the type follows certain expression requirements (for more information about the requirements, see below).

Header
// forwards to <boost/spirit/home/karma/numeric/int.hpp>
#include <boost/spirit/include/karma_int.hpp>

Also, see Include Structure.

Namespace

Name

boost::spirit::lit // alias: boost::spirit::karma::lit

boost::spirit::short_ // alias: boost::spirit::karma::short_

boost::spirit::int_ // alias: boost::spirit::karma::int_

boost::spirit::long_ // alias: boost::spirit::karma::long_

boost::spirit::long_long // alias: boost::spirit::karma::long_long

[Note] Note

The generators long_long and long_long(num) are only available on platforms where the preprocessor constant BOOST_HAS_LONG_LONG is defined (i.e. on platforms having native support for long long (64 bit) integer types).

[Note] Note

lit is reused by the String Generators, the Character Generators, and the Numeric Generators. In general, a char generator is created when you pass in a character, a string generator is created when you pass in a string, and a numeric generator is created when you use a numeric literal.

Synopsis
template <
    typename T
  , unsigned Radix
  , bool force_sign>
struct int_generator;
Template parameters

Parameter

Description

Default

T

The numeric base type of the numeric parser.

int

Radix

The radix base. This can be either 2: binary, 8: octal, 10: decimal and 16: hexadecimal.

10

force_sign

If true, all numbers will have a sign (space for zero)

false

Model of

PrimitiveGenerator

Notation

num

Numeric literal, any signed integer value, or a Lazy Argument that evaluates to a signed integer value of type Num

Num

Type of num: any signed integer type

Radix

A constant integer literal specifying the required radix for the output conversion. Valid values are 2, 8, 10, and 16.

force_sign

A constant boolean literal specifying whether the generated number should always have a sign ('+' for positive numbers, '-' for negative numbers and a ' ' for zero).

Expression Semantics

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

Expression

Semantics

lit(num)

Generate the integer literal num using the default formatting (radix is 10, sign is only printed for negative literals). This generator never fails (unless the underlying output stream reports an error).

short_
int_
long_
long_long

Generate the integer provided by a mandatory attribute using the default formatting (radix is 10, sign is only printed for negative literals). This generator never fails (unless the underlying output stream reports an error).

short_(num)
int_(num)
long_(num)
long_long(num)

Generate the integer provided by the immediate literal value the generator is initialized from using the default formatting (radix is 10, sign is only printed for negative literals). If this generator has an associated attribute it succeeds only if the attribute is equal to the immediate literal (unless the underlying output stream reports an error). Otherwise this generator fails and does not generate any output.

All generators listed in the table above (except lit(num)) are predefined specializations of the int_generator<Num, Radix, force_sign> basic integer number generator type described below. It is possible to directly use this type to create integer generators using a wide range of formatting options.

Expression

Semantics

int_generator<
    Num, Radix, force_sign
>()

Generate the integer of type Num provided by a mandatory attribute using the specified Radix (possible values are 2, 8, 10, and 16, the default value is 10). If force_sign is false (the default), a sign is only printed for negative literals. If force_sign is true, all numbers will be printed using a sign, i.e. '-' for negative numbers, '+' for positive numbers, and ' ' for zeros. This generator never fails (unless the underlying output stream reports an error).

int_generator<
    Num, Radix, force_sign
>()(num)

Generate the integer of type Num provided by the immediate literal value the generator is initialized from, using the specified Radix (possible values are 2, 8, 10, and 16, the default value is 10). If force_sign is false (the default), a sign is only printed for negative literals. If force_sign is true, all numbers will be printed using a sign, i.e. '-' for negative numbers, '+' for positive numbers, and ' ' for zeros. If this generator has an associated attribute it succeeds only if the attribute is equal to the immediate literal (unless the underlying output stream reports an error). Otherwise this generator fails and does not generate any output.

Additional Requirements

The following lists enumerate the requirements which must be met in order to use a certain type Num to instantiate and use a int_generator<Num, Radix, force_sign>.

If boost::is_integral<Num>::value is true the type Num must have defined:

If boost::is_integral<Num>::value is false the type Num must have defined:

Attributes

Expression

Attribute

lit(num)

unused

short_

short, attribute is mandatory (otherwise compilation will fail)

short_(num)

short, attribute is optional, if it is supplied, the generator compares the attribute with num and succeeds only if both are equal, failing otherwise.

int_

int, attribute is mandatory (otherwise compilation will fail)

int_(num)

int, attribute is optional, if it is supplied, the generator compares the attribute with num and succeeds only if both are equal, failing otherwise.

long_

long, attribute is mandatory (otherwise compilation will fail)

long_(num)

long, attribute is optional, if it is supplied, the generator compares the attribute with num and succeeds only if both are equal, failing otherwise.

long_long

long long, attribute is mandatory (otherwise compilation will fail)

long_long(num)

long long, attribute is optional, if it is supplied, the generator compares the attribute with num and succeeds only if both are equal, failing otherwise.

int_generator<
    Num, Radix, force_sign
>()

Num, attribute is mandatory (otherwise compilation will fail)

int_generator<
    Num, Radix, force_sign
>()(num)

Num, attribute is optional, if it is supplied, the generator compares the attribute with num and succeeds only if both are equal, failing otherwise.

[Note] Note

In addition to their usual attribute of type Num all listed generators accept an instance of a boost::optional<Num> as well. If the boost::optional<> is initialized (holds a value) the generators behave as if their attribute was an instance of Num and emit the value stored in the boost::optional<>. Otherwise the generators will fail.

Complexity

O(N), where N is the number of digits needed to represent the generated integer number

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/support_utree.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/proto/deep_copy.hpp>
#include <iostream>
#include <string>

Some using declarations:

using boost::spirit::karma::int_;
using boost::spirit::karma::lit;

Basic usage of an int_ generator:

test_generator("-2", lit(-2));
test_generator("-2", int_(-2));
test_generator_attr("-2", int_(-2), -2);
test_generator_attr("", int_(-2), 3);    // fails (as -2 != 3)!
test_generator_attr("-2", int_, -2);


PrevUpHomeNext