Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2005-07-25 13:28:07


I'm not against the idea of lexical_cast providing std::wstring <->
std::string conversions. However, I think if lexical_cast were to provide
this functionality it would make sense for it to be able to perform
conversions for any locale (argument) and conversions between UTF-8 <->
UTF-16. Otherwise one would need yet another class to perform these
conversions. Personally I think a string_cast class may make more sense.
Just my 2 cents

 

-delfin

 

  _____

From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Sean Rohead
Sent: Friday, July 22, 2005 5:13 PM
To: boost-users_at_[hidden]
Subject: [Boost-users] Missing functions in lexical_cast

 

I would like to convert between std::wstring and std::string using
lexical_cast and finally discovered in an old posting that you must manually
provide template specializations for these types as below. My question is
why these aren't provided in lexical_cast.hpp already?

 

template<> inline std::wstring boost::lexical_cast(std::string source)

{

    std::wstring result(source.size(), wchar_t(0));

    typedef std::ctype<wchar_t> ctype_t;

    const ctype_t& ct = std::use_facet<ctype_t>(std::locale());

    ct.widen(source.data(), source.data() + source.size(),
&(*result.begin()));

    return result;

}

 

 

template<> inline std::string boost::lexical_cast(std::wstring source)

{

    std::string result(source.size(), char(0));

    typedef std::ctype<wchar_t> ctype_t;

    const ctype_t& ct = std::use_facet<ctype_t>(std::locale());

    ct.narrow(source.data(), source.data() + source.size(), '\u00B6',
&(*result.begin()));

    return result;

}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net