Boost logo

Boost Users :

From: Sean Rohead (sean.rohead_at_[hidden])
Date: 2005-07-22 19:13:01


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