lexical-cast

Boost compile error on converting UUID to string using boost::lexical_cast

≡放荡痞女 提交于 2020-01-03 13:35:10
问题 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<std::string>(uuid); but when I am compiling this code, I am getting this error: Source type is neither std::ostream`able nor std::wostream`able C:\Local\boost\boost\lexical_cast\detail\converter_lexical.hpp How can I fix this error? 回答1: You're missing the include, I guess: Live On Coliru #include <boost/lexical_cast.hpp> #include <boost

Enabling Classes for Use with boost::lexical_cast

前提是你 提交于 2020-01-02 02:28:33
问题 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 operator>> (std::istream& i, lexical_castable& le); private: virtual void print_(std::ostream& o) const { o << s_ <<"\n"; } virtual void read_(std::istream& i) const { i >> s_; } std::string s_; }; std::ostream operator<<(std::ostream& o, const lexical_castable

Boost::Lexical_cast conversion to float changes data

☆樱花仙子☆ 提交于 2019-12-24 12:05:22
问题 I am receiving data from MySQL and try to play with it. The data received is in m_caracs and then I try to cut every sub-parts of this stream in other float . Let's see the code : #include <boost/algorithm/string.hpp> #include <boost/lexical_cast.hpp> #include <iostream> #include <vector> #include <string> std::string m_sten; std::string m_feal; std::string m_felt; std::string m_inte; std::string m_sag; std::string m_ende; std::string m_asko; std::string m_vit; void test(bool mon) { std:

boost::lexical_cast not recognizing overloaded istream operator

好久不见. 提交于 2019-12-23 09:39:12
问题 I have the following code: #include <iostream> #include <boost\lexical_cast.hpp> struct vec2_t { float x; float y; }; std::istream& operator>>(std::istream& istream, vec2_t& v) { istream >> v.x >> v.y; return istream; } int main() { auto v = boost::lexical_cast<vec2_t>("1231.2 152.9"); std::cout << v.x << " " << v.y; return 0; } I am receiving the following compile error from Boost: Error 1 error C2338: Target type is neither std::istream able nor std::wistream able This seems straightforward

lexical_cast int to string

会有一股神秘感。 提交于 2019-12-21 07:19:11
问题 Is it safe to ignore exception of boost::lexical_cast when converting int to std::string ? 回答1: Exception raised by lexical cast when converting an int to std::string are not associated to the conversion, but to resource unavailable. So you can ignore this in the same way you ignore the exception bad_alloc raised by operator new. 回答2: As you say, I don't believe the cast can fail for the numerical types for conversion reasons - it can still fail because the string cannot be allocated, of

What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?

最后都变了- 提交于 2019-12-04 15:41:22
问题 What's the purpose of boost::to_string (found in boost/exception/to_string.hpp ) and how does it differ from boost::lexical_cast<std::string> and std::to_string ? 回答1: std::to_string, available since C++11, works on fundamental numeric types specifically. It also has a std::to_wstring variant. It is designed to produce the same results that sprintf would. You may choose this form to avoid dependencies on external libraries/headers. The throw-on-failure function boost::lexical_cast<std::string

lexical_cast int to string

纵饮孤独 提交于 2019-12-04 02:01:23
Is it safe to ignore exception of boost::lexical_cast when converting int to std::string ? Vicente Botet Escriba Exception raised by lexical cast when converting an int to std::string are not associated to the conversion, but to resource unavailable. So you can ignore this in the same way you ignore the exception bad_alloc raised by operator new. As you say, I don't believe the cast can fail for the numerical types for conversion reasons - it can still fail because the string cannot be allocated, of course, but people don't normally catch that error except at the highest level of their code.

Boost spirit floating number parser precision

坚强是说给别人听的谎言 提交于 2019-12-03 21:11:55
问题 There is something strange I noticed when comparing boost::lexical_cast and boost spirit parsing. I'm trying to parse a string into float. for some reason spirit gives very imprecise result. for example: when parsing string "219721.03839999999" using lexical_cast i get 219721.03 which is more or less OK. but when I use spirit (see code below) I get "219721.11" which is far from bein OK. Any idea why it happens? template<> inline float LexicalCastWithTag(const std::string& arg) { float result

What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?

此生再无相见时 提交于 2019-12-03 09:53:54
What's the purpose of boost::to_string (found in boost/exception/to_string.hpp ) and how does it differ from boost::lexical_cast<std::string> and std::to_string ? std::to_string , available since C++11, works on fundamental numeric types specifically. It also has a std::to_wstring variant. It is designed to produce the same results that sprintf would. You may choose this form to avoid dependencies on external libraries/headers. The throw-on-failure function boost::lexical_cast<std::string> and its non-throwing cousin boost::conversion::try_lexical_convert work on any type that can be inserted

Boost spirit floating number parser precision

Deadly 提交于 2019-11-30 21:59:46
There is something strange I noticed when comparing boost::lexical_cast and boost spirit parsing. I'm trying to parse a string into float. for some reason spirit gives very imprecise result. for example: when parsing string "219721.03839999999" using lexical_cast i get 219721.03 which is more or less OK. but when I use spirit (see code below) I get "219721.11" which is far from bein OK. Any idea why it happens? template<> inline float LexicalCastWithTag(const std::string& arg) { float result = 0; if(arg.empty()) { throw BadLexicalCast("Cannot convert from to std::string to float"); } auto