< cpp‎ | locale‎ | wstring convert
Revision as of 20:48, 16 May 2013 by Cubbi (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
explicit wstring_convert( Codecvt* pcvt = new Codecvt );

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
wstring_convert( Codecvt* pcvt, state_type state);

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
explicit wstring_convert( const byte_string& byte_err,
                          const wide_string& wide_err = wide_string() );

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
wstring_convert(const std::wstring_convert&) = delete;

<td > (4) </td> <td > (since C++14) </td> </tr> Template:ddcl list end

1) Constructs the wstring_convert object with a specified conversion facet, using default-constructed values for the shift state and the error strings
2) Constructs the wstring_convert object with a specified conversion facet and specified shift state, using default-constructed values for the error strings
3) Constructs the wstring_convert object with specified error strings, using new Codecvt as the conversion facet and the default-constructed state_type as shift state.
4) The copy constructor is deleted, wstring_convert is not CopyConstructible


pcvt - pointer to the conversion facet of type Codecvt
state - initial value of the conversion shift state
byte_err - narrow string to display on errors
wide_err - wide string to display on errors


#include <locale>
#include <utility>
#include <codecvt>
// utility wrapper to adapt locale-bound facets for wstring/wbuffer convert
template<class Facet>
struct deletable_facet : Facet
    template<class ...Args>
    deletable_facet(Args&& ...args) : Facet(std::forward<Args>(args)...) {}
    ~deletable_facet() {}
int main()
    // UTF-16le / UCS4 conversion
         std::codecvt_utf16<char32_t, 0x10ffff, std::little_endian>
    > u16to32;
    // UTF-8 / wide string conversion with custom messages
    std::wstring_convert<std::codecvt_utf8<wchar_t>> u8towide("Error!", L"Error!");
    // GB18030 / wide string conversion facet
    typedef deletable_facet<std::codecvt_byname<wchar_t, char, std::mbstate_t>> F;
    std::wstring_convert<F> gbtowide(new F("zh_CN.gb18030"));