C++ Tip #9: Lexical Conversions

The smart way to safely cast.


November 01, 2002
URL:http://www.drdobbs.com/c-tip-9-lexical-conversions/184401578

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].

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

Listing 1: How to use lexical_cast

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>

int main() {
  std::string s="3.14159265358979";

  try {
    std::cout << "Lexical conversion from string to double\n";
    double d=boost::lexical_cast<double>(s);
    std::cout << “double d: “ << d << '\n\n';
  }
  catch(boost::bad_lexical_cast& e) {
    std::cout << "Conversion failed: "
              << e.what() << '\n';
  }

  try {
    std::cout << "Lexical conversion from literal string to int\n";
    int i=boost::lexical_cast<int>("123 4");
    std::cout << "int i: " << i << '\n';
  }
  catch(boost::bad_lexical_cast& e) {
    std::cout << "Conversion failed: "
              << e.what() << '\n';
  }
}
— End of Listing —

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.