Channels ▼
RSS

C++ Tip #9: Lexical Conversions


November 2002/C/C++ Tip #9: Lexical Conversions


With unrelenting power, the programmer hacks away at yet another conversion between int and string, string and double, and more. Spread throughout the code like a disease, these conversions, once deemed too simplistic for reuse, are beginning to impact the clarity of the code. The remedy is close at hand, and it’s called lexical_cast.

From the deep forests of Boost [1] come a number of useful C++ class libraries, covering an impressive range of domains — numerical processing, graphs [2, 3], threads [4], metaprogramming, smart pointers [5], and much more. It is here that you find lexical_cast, a small utility for performing lexical conversions consistently and correctly.

lexical_cast

Listing 1 demonstrates how to use lexical_cast.

Listing 1 tries to perform two conversions: one from string to double, and one from a literal string to int. The first conversion is done like this:

double d=boost::lexical_cast<double>(s);

lexical_cast is a templated function, parameterized on the target type of the conversion. It works with all types that are streamable [6]. If a conversion fails, lexical_cast throws an exception of type bad_lexical_cast. In Listing 1, such a failure is provoked by the second call to lexical_cast:

int i=boost::lexical_cast<int>("123 4");

The value "123 4” is clearly not convertible to an int, causing the call to fail. The protection with try/catch must only be omitted when there is absolute certainty that the conversion cannot fail.

Summary

Tedious and repetitive tasks often offer opportunities for reuse. That is definitely the case with lexical conversions, and lexical_cast adds even more value than mere reuse with its elegant resemblance of a C++ cast — the syntax and semantics become obvious. Thanks to Kevlin Henney, father of lexical_cast, you don’t have to do much work to benefit from this fine piece of creative engineering — just visit <www.boost.org> and get on with it!

References

[1] Boost, <www.boost.org>.

[2] Jeremy G. Siek, Lie-Quan Lee, and Andrew Lumsdaine. The Boost Graph Library: User Guide and Reference Manual (Addison-Wesley, 2002).

[3] Vitaly Ablavsky. “Applying BGL to Computational Geometry,” C/C++ Users Journal, August 2002.

[4] Bill Kempf. “The Boost.Threads Library,” C/C++ Users Journal, May 2002.

[5] Bjorn Karlsson. “Smart Pointers in Boost,” C/C++ Users Journal, April 2002.

[6] The resulting type for the conversion must support operator >>, and the source type must support operator <<.

Bjorn Karlsson works as a project manager at ReadSoft. He is the kind of out-going guy who likes to read ISO/IEC 14882:98. Bjorn can be reached at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.