Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) boost::lexical_cast

 

boost::lexical_cast is a Boost function to convert to/from std::string to/from (possibly) any data type. CanLexicalCast can check if this conversion is possible.

 

The C++11 equivalent to convert to a std::string is std::to_string.

 

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

int main()
{
  const std::string s = "12.34";
  const double d = boost::lexical_cast<double>(s);
}

 

LexicalCast serves the same purpose as boost::lexical_cast, but does not use Boost and does not throw an exception when conversion fails. Note that there are differences between LexicalCast and boost::lexical_cast, as boost::lexical_cast is more strict.

 

 

 

 

 

External links

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict