string/wstring conversion using lexical_cast
Hi, I'm using Boost 1.31.0 with VC++ 7.1 and I'm having some problems converting std::string to std::wstring and back. Here's the code that I'm trying to get to compile: std::wstring x = boost::lexical_cast<std::wstring>( std::string("this doesn't work")); std::string y = boost::lexical_cast<std::string>( std::wstring(L"neither does this")); The error I get for string to wstring: lexical_cast.hpp(150): error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::wstring' (or there is no acceptable conversion) And the errors I get for wstring to string: lexical_cast.hpp(144): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion) with [ _Elem=wchar_t, _Traits=std::char_traits<wchar_t>, _Ax=std::allocator<wchar_t> ] lexical_cast.hpp(144): error C2228: left of '.fail' must have class/struct/union type Any ideas of what I'm doing wrong? Best Regards, Tony Djordjevski
In article <CLEKIHLHCHBBNPGGHFMBKEHBCIAA.tony@v-sim.com>, "Tony Djordjevski" <tony@v-sim.com> wrote:
I'm using Boost 1.31.0 with VC++ 7.1 and I'm having some problems converting std::string to std::wstring and back.
You can't do that. lexical_cast from X to Y requires that there is a stream to which X can be written and from which Y can be read. However, string can only be read from basic_stream<char> and wstring can only be written to basic_stream<wchar_t>. However, if you add an operator that can read a wstring from a stream<char>, and one that can read a string from a stream<wchar_t>, then it will work. hth meeroh -- If this message helped you, consider buying an item from my wish list: <http://web.meeroh.org/wishlist>
participants (2)
-
Miro Jurisic
-
Tony Djordjevski