lexical-cast

Convert C++Builder AnsiString to std::string via boost::lexical_cast

For a school assignment I have to implement a project in C++ using Borland C++ Builder. As the VCL uses AnsiString for all GUI Components I have to convert all of my std::strings to AnsiString for the sake of displaying. std::string inp = "Hello world!"; AnsiString outp(inp.c_str()); works of course but is a bit tedious to write and ...

Very poor boost::lexical_cast performance

Hello, Windows XP SP3. Core 2 Duo 2.0 GHz. I'm finding the boost::lexical_cast performance to be extremely slow. Wanted to find out ways to speed up the code. Using /O2 optimizations on visual c++ 2008 and comparing with java 1.6 and python 2.6.2 I see the following results. Integer casting: c++: std::string s ; for(int i = 0; i < 10...

How can I extend a lexical cast to support enumerated types?

I have the following function that will convert a string into a numeric data type: template <typename T> bool ConvertString(const std::string& theString, T& theResult) { std::istringstream iss(theString); return !(iss >> theResult).fail(); } This does not work for enumerated types, however, so I have done something like this: ...

How to use the boost lexical_cast library for just for checking input

I use the boost lexical_cast library for parsing text data into numeric values quite often. In several situations however, I only need to check if values are numeric; I don't actually need or use the conversion. So, I was thinking about writing a simple function to test if a string is a double: template<typename T> bool is_double(cons...

Combine boost::lexical_cast and std::transform

I would like to write something like this, which cannot be compiled: std::vector<A> as; std::vector<B> bs( as.size() ); std::transform( as.beginn(), as.end(), bs.begin(), boost::lexical_cast<B> ); But this is not working, so I created a functor which is doing this for me: template<typename Dest> struct lexical_transform { templat...

boost lexical_cast throws exception

Hi I'm using boost libs for c++ and the function lexical_cast behaves really weird. If I do lexical_cast("0.07513994") it works fine, but if I use my variable which I need to convert, it throws the bad_lexical_cast exception. Here is the code: string word; istringstream iss(line); do { string word; iss >> word; double ...

lexical_cast int to string

Is it safe to ignore exception of boost::lexical_cast when converting int to std::string? ...

lex_cast: Make formatted streams, unformatted

I once saw this nice little snippet of code below, here at SO: template<typename to_t, typename from_t> to_t lex_cast(const from_t &arg) { to_t ret; std::stringstream os; os << arg; os >> ret; return ret; } This mimics boost::lexical_cast. Usage: string tmp = ::lex_cast<string>(3.14); However, due to the default...