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@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sean Rohead
Sent: Friday, July 22, 2005 5:13 PM
To: boost-users@lists.boost.org
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;

}