C ++ Boost Lexical

zhaozj2021-02-08  274

HEADER BOOST / LEXical_cast.hpp

Motivation example Outline Lexical_cast Bad_lexical_cast portability future direction

Motivation In many cases we have to turn a value to characters, just like INT performance is a string, or in contrast, a string is interpreted as an int. When needed to convert data in various types in various types inside or outside the program This example is very common, such as Windows and profiles.

Standard C and C provide a lot of smart conversion methods. However, they are different in ease of use, scalability, security, etc.

For example, there is a lot of limitations of standard C-function families represented by ATOI.

The conversion between the string to the internal data type can only be performed one-way. When the library function using C is converted, the use of inconvenient, securely compromised Sprintf; either use non-standard functions, such as ITOA, thereby losing transplantability. Convertible type range is limited to built-in digital type, that is, Int, long, and double. There is no common form to extend the type range that can be converted. For example, switch from the String type to the plurality and aligns. The standard C function represented by STRTOL has the same limit, but it provides a better conversion process control. However, such controls are often unwanted or not used. The Scanf function family provides more powerful control while also serious lack of security and ease of use.

Standard C provides StringStream to convert. It provides a lot of process control for I / O and any type to string conversion and formatting. However, the use of StringStream for very awkward conversion (additional local variables), which cannot be embedded in an expression) and (temporary objects for creating StringStream when used in expressions). Facets provides a wide range of conceptual support and convenience of controlling text, but its high threshold requires very high intervention for simple conversion.

The Lexical_cast template method provides a convenient and consistent form of conversions from any type to string or opposite. The simplification of this conversion provides a convenience of expression levels. For more intervention conversions, such as the need for accuracy and formatting control than Lexical_cast, it is recommended to use a conventional StringStream path. For conversions from numbers to numbers, Numeric_cast may provide more reasonable conversion than Lexical_cast.

Example The following example converts command line parameters into a digital sequence

Int main (int Argc, char * argv [])

{

Using boost :: lexical_cast;

Using boost :: bad_lexical_cast;

Std :: Vector args;

While (* Argv)

{

Try

{

Args.push_back (lexical_cast (* argv));

}

Catch (Bad_Lexical_cast &)

{

Args.push_back (0);

}

}

...

}

The following example converts digital data to string

Void log_message (const st :: string);

Void log_errno (int yoko)

{

LOG_MESSAGE ("Error" Boost :: Lexical_cast (yoko) ":" strerror (Yoko));

}

Characteristic definition of the outline library

"Boost / Lexical_cast.hpp":

Namespace Boost

{

Class Bad_lexical_cast;

Template Target Lexical_cast (Source Arg);

}

Test case definition

"lexical_cast_test.cpp".

Lexical_cast

Template

Target Lexical_cast (Source Arg);

Returned

ARG flows

Std :: stringstream and then act

The result of the Target object taken out. Conversion is currently

Lexical_context parameterized. If the conversion is unsuccessful, one

Bad_lexical_cast exception is thrown, otherwise a Target object is returned.

The requirements for parameters and results are as follows:

Source is a stream output, that is, providing a left side is std :: ostream object, the right side is the Operator << operator implementation of a parameter type instance. Both Source and Targets are copied (providing copy constructor) [20.1.3]. Target is a stream input, that is, the left side is std :: istream object right is the Operator> of the result type instance> > Operators implementation. Target is constructed by default, that is, the default initialization a type object is possible (providing the default constructor) [8.5, 20.1.3]. Target is the value [23.1].

BAD_LEXICAL_CAST

Class Bad_lexical_cast: public std :: bad_cast

{

PUBLIC:

Virtual const char * what () const throw ();

}

Exceptions to indicate runtime

Lexical_cast failed.

Transplantable to code and test frames are compiled by Microsoft Visual C 6.0, Borland C 5.5, and GNU G 2.91. The test under Microsoft Visual C 6.0 and Borland C 5.5 is very successful. In addition to G stream explains any shaping as a legitimate BOOL type, not just

0 and

1. Other tests have not been issued without problems. In order to support OUT-OF-a-Box G , standard header files not recommended

will ratio

Priority.

Future direction

A mechanism that provides service quality control, such as a formatting and exception behavior. In the principle of simple (easy to publish), the current version removes an early experiment. Wide characters and incompatible std :: Basic_String issues urgently need to be resolved. An interpret_cast can be required to perform DO-Something-Reasonable transition between data types. For example, it will automatically select the right one between Numeric_Cast and Lexical_Cast based on std :: numeric_limits <> :: is_specialized.

© Copyright Kevlin Henney, 2000

© Chinese translation by Lythm, 2002

转载请注明原文地址:https://www.9cbs.com/read-2303.html

New Post(0)