...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
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).
// forwards to <boost/spirit/home/karma/numeric/int.hpp> #include <boost/spirit/include/karma_int.hpp>
Also, see Include Structure.
Name |
---|
|
|
|
|
|
![]() |
Note |
---|---|
The generators |
![]() |
Note |
---|---|
|
template < typename T , unsigned Radix , bool force_sign> struct int_generator;
Parameter |
Description |
Default |
---|---|---|
|
The numeric base type of the numeric parser. |
|
|
The radix base. This can be either 2: binary, 8: octal, 10: decimal and 16: hexadecimal. |
10 |
|
If |
|
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).
Semantics of an expression is defined only where it differs from, or
is not defined in PrimitiveGenerator
.
Expression |
Semantics |
---|---|
|
Generate the integer literal |
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 |
int_generator< Num, Radix, force_sign >()(num)
|
Generate the integer of type |
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:
<
,
<=
, ==
, !=
,
>
, and >=
+
,
-
, /
,
*
, %
,
and unary -
If boost::is_integral<Num>::value
is false
the type Num
must have
defined:
<
,
<=
, ==
, !=
,
>
, and >=
+
,
-
, /
,
*
, %
,
and unary -
std::fmod
, std::fabs
,
std::pow
, std::lround
,
std::ltrunc
, std::floor
,
and std::ceil
. These need to be defined
in a way so that they will be found using argument dependent lookup
(ADL).
Expression |
Attribute |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int_generator< Num, Radix, force_sign >()
|
|
int_generator< Num, Radix, force_sign >()(num)
|
|
![]() |
Note |
---|---|
In addition to their usual attribute of type |
O(N), where
N
is the number of digits needed to represent the generated integer number
![]() |
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::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);