std::wbuffer_convert

From Cppreference

Jump to: navigation, search
Defined in header <locale>

template<class Codecvt,

         class Elem = wchar_t,
         class Tr = std::char_traits<Elem> >

class wbuffer_convert;
(C++11 feature)

std::wbuffer_convert is a wrapper over stream buffer of type std::basic_streambuf<char> which gives it the appearance of std::basic_streambuf<Elem>. All I/O performed through std::wbuffer_convert undergoes character conversion as defined by the facet Codecvt. std::wbuffer_convert assumes ownership of the conversion facet, and cannot use a facet managed by a locale. The standard facets suitable for use with std::wbuffer_convert are std::codecvt_utf8 for UTF-8/UCS2 and UTF-8/UCS4 conversions and std::codecvt_utf8_utf16 for UTF-8/UTF-16 conversions.

Contents

[edit] Member types

Member type Definition
state_type Codecvt::state_type

[edit] Member functions

(constructor)
constructs a new wbuffer_convert
(public member function)
(destructor)
destructs the wbuffer_convert and its conversion facet
(public member function)
rdbuf
returns or replaces the underlying narrow stream buffer
(public member function)
state
returns the current shift state
(public member function)

[edit] Example

The following example demonstrates the use of wbuffer_convert for input and output on a system with 32-bit wchar_t

#include <iostream>
#include <sstream>
#include <string>
#include <locale>
#include <codecvt>
int main()
{
    // Example 1: wrap a UTF-8 string stream in a UCS4 wbuffer_convert
    //
    // UTF-8 narrow multibyte encoding:
    std::string utf8 = u8"z\u00df\u6c34\U0001d10b";  // or u8"zß水𝄋"
                                               // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    std::istringstream utf8buf(utf8);
    std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv_in(utf8buf.rdbuf());
    std::wistream ucsbuf(&conv_in);
    std::cout << "Reading from a UTF-8 istringstream via wbuffer_convert produces\n";
    for(wchar_t c; ucsbuf.get(c); )
        std::cout << std::hex << std::showbase << c << '\n';
 
    // Example 2: wrap a UTF-8 aware std::cout in a UCS4 wbuffer_convert to output UCS4
    //
    std::wbuffer_convert<std::codecvt_utf8<wchar_t>> conv_out(std::cout.rdbuf());
    std::wostream out(&conv_out);
    std::cout << "Sending UCS4 data to std::cout via wbuffer_convert:\n";
    out << L"z\u00df\u6c34\U0001d10b\n";
}

Output:

Reading from a UTF-8 istringstream via wbuffer_convert produces
0x7a
0xdf
0x6c34
0x1d10b
Sending UCS4 data to std::cout via wbuffer_convert:
zß水𝄋

[edit] See also

Character
conversions
narrow multibyte
(char)
UTF-8
(char)
UTF-16
(char16_t)
UTF-16 mbrtoc16 / c16rtombr codecvt<char16_t, char, mbstate_t>
codecvt_utf8_utf16<char16_t>
codecvt_utf8_utf16<char32_t>
codecvt_utf8_utf16<wchar_t>
N/A
UCS2 No codecvt_utf8<char16_t> codecvt_utf16<char16_t>
UTF-32/UCS4
(char32_t)
mbrtoc32 / c32rtombr codecvt<char32_t, char, mbstate_t>
codecvt_utf8<char32_t>
codecvt_utf16<char32_t>
UCS2/UCS4
(wchar_t)
No codecvt_utf8<wchar_t> codecvt_utf16<wchar_t>
wide
(wchar_t)
codecvt<wchar_t, char, mbstate_t>
mbstowcs / wcstombs
No No
wstring_convert (C++11)
performs conversions between a wide string and a byte string
(class template)
codecvt_utf8 (C++11)
converts between UTF-8 and UCS2/UCS4
(class template)
codecvt_utf8_utf16 (C++11)
converts between UTF-8 and UTF-16
(class template)