Questions tagged [lexical-cast]
69 questions
2
votes
2 answers
boost lexical cast check
This should be an easy one. I have a function that traverses a csv and tokenizes based on commas and does things with the tokens. One of these things is convert it into an int. Unfortunately, the first token may not always be an int, so when it is…

Lexicon
- 2,467
- 7
- 33
- 41
2
votes
1 answer
Alternative to lexical_cast(std::string)
I've got templated code that uses lexical_cast.
Now I want to remove all the lexical_cast calls (because it doesn't work well with /clr).
I need to cast object between std::string and their value.
So, the first direction is easy (T _from,…

Yochai Timmer
- 48,127
- 24
- 147
- 185
2
votes
3 answers
C++ boost lexical_cast with template?
I'm trying to build a class that stores program settings as a std::map. Since all the program settings are stored as strings I'd like an accessor method that can return the program setting casted to the relevant type. I'm new to templating in C++…

User
- 62,498
- 72
- 186
- 247
2
votes
2 answers
stringstream: why does "showpoint" behave similar as "fixed"?
I'd like to write my own lexical_cast which preserves the decimal point when converting double to std::string. So I'm using ostringstream and set the flag std::ios::showpoint:
#include
#include
#include…

tom
- 333
- 4
- 12
2
votes
1 answer
lex_cast: Make formatted streams, unformatted
I once saw this nice little snippet of code below, here at SO:
template
to_t lex_cast(const from_t &arg) {
to_t ret;
std::stringstream os;
os << arg;
os >> ret;
return ret;
}
This mimics…

eisbaw
- 2,678
- 2
- 19
- 18
2
votes
1 answer
How can I use boost::lexical_cast with folly::fbstring?
The following program:
#include
#include
#include
#include
class foo { };
std::ostream& operator<<(std::ostream& stream, const foo&) {
return stream << "hello…

Tavian Barnes
- 12,477
- 4
- 45
- 118
2
votes
1 answer
When catching a boost::bad_lexical_cast, can I access the string/token which was to be cast?
I'm running code which might throw a boost:bad_lexical_cast when casting a sequence of tokens - but I can't go into the code and "put the tokens aside" so I can figure out what cast actually failed.
Does boost:bad_lexical_cast let you access that…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
1 answer
How to avoid repeated istringstream construction when converting stream of string tokens
I'm (intending to) use the code in this answer to read something from a CSV. Essentially I get an iterator to strings between consecutive , characters; but instead of putting them in a vector of strings, I want to parse these strings into elements…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
2 answers
Is boost::lexical_cast thread-safe?
I am actually failing to find an answer to this question in boost documentation. I am being a bit paranoid about using atof in a multi-threaded environment, so one suggestion was to replace the call with lexical_cast. Is lexical_cast thread-safe?

sneg
- 2,088
- 4
- 19
- 23
2
votes
2 answers
When should I use Boost's lexical_cast? Is it a mechanism of last resort?
Boost's lexical_cast converts numbers (or arbitrary objects) to strings and back. Should I try to use it more, say, instead of streaming things to std::stringstreams? Or is it more of a mechanism-of-last-resort?
Here's one example of two alternative…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
3 answers
boost lexical_cast throws exception
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…

Student
- 317
- 1
- 8
- 16
1
vote
1 answer
boost::lexical_cast from string to char exception
I am new to using boost::lexical_cast and have minimal understanding of its internals. I am trying to do the following cast:
string someString = boost::lexical_cast(sourceString);
However, boost is complaining that the above code…

czchlong
- 2,434
- 10
- 51
- 65
1
vote
1 answer
std::istream extraction sets failbit for no apparent reason
I'm creating a primitive type wrapper, which can use boost::lexical_cast for setting its value from a string. It works fine, but for some reason std::istream extraction operator sets the failbit. The following program prints:
123.45
EXCEPTION:…

zeroes00
- 531
- 3
- 16
1
vote
1 answer
boost bad lexical cast: source type value could not be interpreted as target when converting a string to unsigned long long
the following piece of code that I compiled on wandbox.org is causing the following error. I don't understand why I am getting the error.
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include
#include…

gringo
- 373
- 2
- 4
- 15
1
vote
1 answer
boost::lexical_cast with boost::units::quantity does not compile any more
I have a problem with code that parses values taken via boost_program_options to boost::units quantities. It used to work just fine, and now, on a new setup, no longer does. The obscure error message hints at a problem with boost::lexical_cast,…

fedro
- 13
- 3