Questions tagged [lexical-cast]
69 questions
47
votes
9 answers
Very poor boost::lexical_cast performance
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…

Naveen
- 4,456
- 2
- 22
- 16
26
votes
2 answers
What's the difference between std::to_string, boost::to_string, and boost::lexical_cast?
What's the purpose of boost::to_string (found in boost/exception/to_string.hpp) and how does it differ from boost::lexical_cast and std::to_string?

Claudiu
- 224,032
- 165
- 485
- 680
22
votes
3 answers
How do I use boost::lexical_cast and std::boolalpha? i.e. boost::lexical_cast< bool >("true")
I've seen some answers to other boost::lexical_cast questions that assert the following is possible:
bool b = boost::lexical_cast< bool >("true");
This doesn't work for me with g++ 4.4.3 boost 1.43. (Maybe it's true that it works on a platform…

poindexter
- 1,269
- 3
- 12
- 23
22
votes
2 answers
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
bool ConvertString(const std::string& theString, T& theResult)
{
std::istringstream iss(theString);
return !(iss >>…

James McNellis
- 348,265
- 75
- 913
- 977
13
votes
3 answers
lexical_cast int to string
Is it safe to ignore exception of boost::lexical_cast when converting int to std::string?

dimba
- 26,717
- 34
- 141
- 196
12
votes
3 answers
Why does lexical_cast require the operator>> to be in a matching namespace?
Here's a testcase:
#include
#include
namespace N {
enum class alarm_code_t {
BLAH
};
}
std::istream& operator>>(std::istream& is, N::alarm_code_t& code)
{
std::string tmp;
is >> tmp;
…

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
9
votes
3 answers
When does boost::lexical_cast to std::string fail?
I'm writing unit tests and trying to have all my code covered.
I have in my code something like this:
template
std::string ConvertToStringUsingBoost(ValueType const& v)
{
try
{
return…

Grigorii Alekseev
- 177
- 2
- 11
9
votes
1 answer
boost::lexical_cast not recognizing overloaded istream operator
I have the following code:
#include
#include
struct vec2_t
{
float x;
float y;
};
std::istream& operator>>(std::istream& istream, vec2_t& v)
{
istream >> v.x >> v.y;
return istream;
}
int…

Colin Basnett
- 4,052
- 2
- 30
- 49
9
votes
1 answer
C++ Using classes with boost::lexical_cast
I want to use my Test class with boost::lexical_cast. I have overloaded operator<< and operator>> but It gives me runtime error.
Here is my code:
#include
#include
using namespace std;
class Test {
int a,…

Kia.celever
- 635
- 2
- 9
- 16
8
votes
1 answer
Enabling Classes for Use with boost::lexical_cast
Code snippet from lexical_cast:
class lexical_castable {
public:
lexical_castable() {};
lexical_castable(const std::string s) : s_(s) {};
friend std::ostream operator<<
(std::ostream& o, const lexical_castable& le);
friend std::istream…

q0987
- 34,938
- 69
- 242
- 387
6
votes
2 answers
Combine boost::lexical_cast and std::transform
I would like to write something like this, which cannot be compiled:
std::vector as;
std::vector bs( as.size() );
std::transform( as.beginn(), as.end(), bs.begin(), boost::lexical_cast );
But this is not working, so I created a functor…

Mathias Soeken
- 1,293
- 2
- 8
- 22
5
votes
2 answers
boost::lexical_cast can convert hex inside string to int?
I see in this topic C++ convert hex string to signed integer that boost::lexical_cast can convert hexadecimal inside string to another type (int, long...)
but when I tried this code:
std::string s = "0x3e8";
try {
auto i =…

Arthur
- 159
- 7
5
votes
1 answer
Using boost::lexical_cast with std::transform
g++ doesn't like:
vector x;
x += 1,2,3,4,5;
vector y(x.size());
transform(x.begin(), x.end(), y.begin(), lexical_cast);
The error message is:
error: no matching function for call to…

Frank
- 4,341
- 8
- 41
- 57
5
votes
2 answers
Boost compile error on converting UUID to string using boost::lexical_cast
I have this code which is based on several posts in SO:
boost::uuids::uuid uuid = boost::uuids::random_generator()();
auto uuidString= boost::lexical_cast(uuid);
but when I am compiling this code, I am getting this error:
Source type…

mans
- 17,104
- 45
- 172
- 321
5
votes
1 answer
static_cast vs boost::lexical_cast
I am trying to concatenate an integer to a known string, and I have found that there are several ways to do it, two of those being:
int num=13;
string str = "Text" + static_cast( &(ostringstream() << num) )->str();
or I could also…

joaocandre
- 1,621
- 4
- 25
- 42