public member function
<locale>

std::wstring_convert::wstring_convert

default / initialization (1)
wstring_convert (Codecvt* pcvt = new Codecvt);
state (2)
wstring_convert (Codecvt* pcvt, state_type state);
error strings (3)
wstring_convert (const byte_string& byte_err,                 const wide_string& wide_err = wide_string());
default / initialization (1)
explicit wstring_convert (Codecvt* pcvt = new Codecvt);
state (2)
         wstring_convert (Codecvt* pcvt, state_type state);
error strings (3)
explicit wstring_convert (const byte_string& byte_err,                 const wide_string& wide_err = wide_string());
copy [deleted] (4)
         wstring_convert (const wstring_convert&) = delete;
Construct wstring_convert
Constructs a wstring_convert object:

(1) default constructor / initialization constructor
Constructs an object that uses pcvt as conversion object with a default-constructed shift state (the shift state is reset before every conversion operation).
(2) initialization constructor with state
Constructs an object that uses pcvt as conversion object and state as the initial value for its shift state (the shift state is kept between conversion operations).
(3) constructor with error strings
Constructs an object that returns byte_err or wide_err on failure to convert, instead of throwing an exception.
The object uses a conversion object automatically constructed with new Codecvt whose shift state is reset before every conversion operation.
(4) copy constructor [deleted]
wstring_convert objects cannot be copied (both the copy-constructor and copy-assignment are deleted). [C++14]

Parameters

pcvt
Pointer to a conversion object (such as those of the types declared in <codecvt>) whose storage has already been allocated with new.
The constructed object acquires ownership of this conversion object, which is automatically destroyed (with operator delete) when this object is destroyed. Note that this makes facets managed by locale objects not suitable to be used by wstring_convert.
If a null pointer is passed, it causes undefined behavior.
Codecvt is the first template parameter of wstring_convert (its conversion object's type).
state
Initial conversion shift state. The state is carried onto the next conversion operation.
state_type is a member type, defined as an alias of Codecvt::state_type (where Codecvt is the first template parameter of wstring_convert).
byte_err
A narrow string to return on errors, instead of throwing.
byte_string is a member type, defined as an alias of basic_string<char,char_traits<char>,Byte_alloc> (where Byte_alloc is the fourth template parameter of wstring_convert).
wide_err
A wide string to return on errors, instead of throwing.
wide_string is a member type, defined as an alias of basic_string<Elem,char_traits<Elem>,Wide_alloc> (where Elem and Wide_alloc are the second and third template parameters of wstring_convert, respectivelly).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// wstring_convert constructors
#include <iostream>       // std::cout
#include <string>         // std::string, std::wstring
#include <locale>         // std::wstring_convert
#include <codecvt>        // std::codecvt_utf8

int main ()
{
  typedef std::codecvt_utf8<wchar_t> ccvt;

  std::wstring_convert<ccvt> foo;
  std::wstring_convert<ccvt> bar (new ccvt, ccvt::state_type());
  std::wstring_convert<ccvt> baz ("[error]",L"[error]");

  std::wstring wstr (L"test");
  std::string str = baz.to_bytes (wstr);

  std::cout << str << '\n';

  return 0;
}

Output:

test


Data races

Management of the storage for the element pointed by pcvt (if any) is acquired by the constructed object.

Exception safety

Strong guarantee: no effects in case an exception is thrown.

See also