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 Parsers (int_, etc.)
Description

The int_parser can parse signed integers of arbitrary length and size. This is almost the same as the uint_parser. The only difference is the additional task of parsing the '+' or '-' sign preceding the number. The class interface is the same as that of the uint_parser.

The int_parser parser can be used to parse ordinary primitive C/C++ integers or even user defined scalars such as bigints (unlimited precision integers) as long as the type follows certain expression requirements (documented below).

Header
// forwards to <boost/spirit/home/qi/numeric/int.hpp>
#include <boost/spirit/include/qi_int.hpp>

Also, see Include Structure.

Namespace

Name

boost::spirit::lit // alias: boost::spirit::qi::lit

boost::spirit::short_ // alias: boost::spirit::qi::short_

boost::spirit::int_ // alias: boost::spirit::qi::int_

boost::spirit::long_ // alias: boost::spirit::qi::long_

boost::spirit::long_long // alias: boost::spirit::qi::long_long

[Note] Note

long_long is only available on platforms where the preprocessor constant BOOST_HAS_LONG_LONG is defined (i.e. on platforms having native support for signed long long (64 bit) unsigned integer types).

[Note] Note

lit is reused by the Character Parsers, and the Numeric Parsers. In general, a char parser is created when you pass in a character, and a numeric parser is created when you use a numeric literal.

Synopsis
template <
    typename T
  , unsigned Radix
  , unsigned MinDigits
  , int MaxDigits>
struct int_parser;
Template parameters

Parameter

Description

Default

T

The numeric base type of the numeric parser.

none

Radix

The radix base. This can be any base from 2..10 and 16

10

MinDigits

The minimum number of digits allowable.

1

MaxDigits

The maximum number of digits allowable. If this is -1, then the maximum limit becomes unbounded.

-1

Model of

PrimitiveParser

Notation

n

An object of T, the numeric base type.

num

Numeric literal, any signed integer value, or a Lazy Argument that evaluates to a signed integer value.

Expression Semantics

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

Expression

Semantics

short_
int_
long_
long_long

Parse a signed integer using the default radix (10).

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

Match the literal num using the default radix (10). The parser will fail if the parsed value is not equal to the specified value.

int_parser<
    T, Radix, MinDigits, MaxDigits
>()

Parse a signed integer of type T using radix Radix, with a minimum of MinDigits and a maximum of MaxDigits.

int_parser<
    T, Radix, MinDigits, MaxDigits
>()(num)

Match the literal num of type T using radix Radix, with a minimum of MinDigits and a maximum of MaxDigits. The parser will fail if the parsed value is not equal to the specified value.

[Important] Important

All numeric parsers check for overflow conditions based on the type T the corresponding int_parser<> has been instantiated with. If the parsed number overflows this type the parsing fails. Please be aware that the overflow check is not based on the type of the supplied attribute but solely depends on the template parameter T.

Attributes

Expression

Attribute

lit(num)

unused

short_
short_(num)

short

int_
int_(num)

int

long_
long_(num)

long

long_long
long_long(num)

boost::long_long_type

int_parser<
    T, Radix, MinDigits, MaxDigits
>()
int_parser<
    T, Radix, MinDigits, MaxDigits
>()(num)

T

Complexity

O(N), where N is the number of digits being parsed plus the sign.

Minimum Expression Requirements for T

For the numeric base type, T, the expression requirements below must be valid:

Expression

Semantics

T()

Default construct.

T(0)

Construct from an int.

n + n

Addition.

n - n

Subtraction.

n * n

Multiplication.

std::numeric_limits<T>::is_bounded

true or false if T bounded.

std::numeric_limits<T>::digits

Maximum Digits for T, radix digits. Required only if T is bounded.

std::numeric_limits<T>::digits10

Maximum Digits for T, base 10. Required only if T is bounded.

std::numeric_limits<T>::max()

Maximum value for T. Required only if T is bounded.

std::numeric_limits<T>::min()

Minimum value for T. Required only if T is bounded.

Example
[Note] Note

The test harness for the example(s) below is presented in the Basics Examples section.

Some using declarations:

using boost::phoenix::val;
using boost::spirit::qi::lit;
using boost::spirit::qi::int_;

Basic signed integers:

// signed int
test_parser("+12345", int_);
test_parser("-12345", int_);
test_parser("+12345", int_(12345));
test_parser("-12345", int_(-12345));
test_parser("+12345", int_(val(12345)));
test_parser("-12345", int_(val(-12345)));

// literals
test_parser("+12345", lit(12345));
test_parser("-12345", lit(-12345));
test_parser("+12345", lit(val(12345)));
test_parser("-12345", lit(val(-12345)));


PrevUpHomeNext