Class template
wstring_convert performs conversions between a wide
string and a byte string
. It lets you specify a code conversion facet
(like class template
codecvt) to perform the conversions, without
affecting any streams or locales
. [
Example 1:
If you want to use the code
conversion facet codecvt_utf8 to output to cout a UTF-8
multibyte sequence corresponding to a wide string, but you don't want to
alter the locale for cout, you can write something like:
wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
std::string mbstring = myconv.to_bytes(L"Hello\n");
std::cout << mbstring;
—
end example]
namespace std {
template<class Codecvt, class Elem = wchar_t,
class WideAlloc = allocator<Elem>,
class ByteAlloc = allocator<char>>
class wstring_convert {
public:
using byte_string = basic_string<char, char_traits<char>, ByteAlloc>;
using wide_string = basic_string<Elem, char_traits<Elem>, WideAlloc>;
using state_type = typename Codecvt::state_type;
using int_type = typename wide_string::traits_type::int_type;
wstring_convert() : wstring_convert(new Codecvt) {}
explicit wstring_convert(Codecvt* pcvt);
wstring_convert(Codecvt* pcvt, state_type state);
explicit wstring_convert(const byte_string& byte_err,
const wide_string& wide_err = wide_string());
~wstring_convert();
wstring_convert(const wstring_convert&) = delete;
wstring_convert& operator=(const wstring_convert&) = delete;
wide_string from_bytes(char byte);
wide_string from_bytes(const char* ptr);
wide_string from_bytes(const byte_string& str);
wide_string from_bytes(const char* first, const char* last);
byte_string to_bytes(Elem wchar);
byte_string to_bytes(const Elem* wptr);
byte_string to_bytes(const wide_string& wstr);
byte_string to_bytes(const Elem* first, const Elem* last);
size_t converted() const noexcept;
state_type state() const;
private:
byte_string byte_err_string;
wide_string wide_err_string;
Codecvt* cvtptr;
state_type cvtstate;
size_t cvtcount;
};
}