From Jason Turner

[depr.conversions.string]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpu2vde9_b/{from.md → to.md} +204 -0
tmp/tmpu2vde9_b/{from.md → to.md} RENAMED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Class template `wstring_convert` <a id="depr.conversions.string">[[depr.conversions.string]]</a>
2
+
3
+ Class template `wstring_convert` performs conversions between a wide
4
+ string and a byte string. It lets you specify a code conversion facet
5
+ (like class template `codecvt`) to perform the conversions, without
6
+ affecting any streams or locales.
7
+
8
+ [*Example 1*:
9
+
10
+ If you want to use the code conversion facet `codecvt_utf8` to output to
11
+ `cout` a UTF-8 multibyte sequence corresponding to a wide string, but
12
+ you don’t want to alter the locale for `cout`, you can write something
13
+ like:
14
+
15
+ ``` cpp
16
+ wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
17
+ std::string mbstring = myconv.to_bytes(L"Hello\n");
18
+ std::cout << mbstring;
19
+ ```
20
+
21
+ — *end example*]
22
+
23
+ ``` cpp
24
+ namespace std {
25
+ template <class Codecvt, class Elem = wchar_t,
26
+ class Wide_alloc = allocator<Elem>,
27
+ class Byte_alloc = allocator<char>>
28
+ class wstring_convert {
29
+ public:
30
+ using byte_string = basic_string<char, char_traits<char>, Byte_alloc>;
31
+ using wide_string = basic_string<Elem, char_traits<Elem>, Wide_alloc>;
32
+ using state_type = typename Codecvt::state_type;
33
+ using int_type = typename wide_string::traits_type::int_type;
34
+
35
+ explicit wstring_convert(Codecvt* pcvt = new Codecvt);
36
+ wstring_convert(Codecvt* pcvt, state_type state);
37
+ explicit wstring_convert(const byte_string& byte_err,
38
+ const wide_string& wide_err = wide_string());
39
+ ~wstring_convert();
40
+
41
+ wstring_convert(const wstring_convert&) = delete;
42
+ wstring_convert& operator=(const wstring_convert&) = delete;
43
+
44
+ wide_string from_bytes(char byte);
45
+ wide_string from_bytes(const char* ptr);
46
+ wide_string from_bytes(const byte_string& str);
47
+ wide_string from_bytes(const char* first, const char* last);
48
+
49
+ byte_string to_bytes(Elem wchar);
50
+ byte_string to_bytes(const Elem* wptr);
51
+ byte_string to_bytes(const wide_string& wstr);
52
+ byte_string to_bytes(const Elem* first, const Elem* last);
53
+
54
+ size_t converted() const noexcept;
55
+ state_type state() const;
56
+
57
+ private:
58
+ byte_string byte_err_string; // exposition only
59
+ wide_string wide_err_string; // exposition only
60
+ Codecvt* cvtptr; // exposition only
61
+ state_type cvtstate; // exposition only
62
+ size_t cvtcount; // exposition only
63
+ };
64
+ }
65
+ ```
66
+
67
+ The class template describes an object that controls conversions between
68
+ wide string objects of class `basic_string<Elem, char_traits<Elem>,
69
+ Wide_alloc>` and byte string objects of class `basic_string<char,
70
+ char_traits<char>, Byte_alloc>`. The class template defines the types
71
+ `wide_string` and `byte_string` as synonyms for these two types.
72
+ Conversion between a sequence of `Elem` values (stored in a
73
+ `wide_string` object) and multibyte sequences (stored in a `byte_string`
74
+ object) is performed by an object of class `Codecvt`, which meets the
75
+ requirements of the standard code-conversion facet `codecvt<Elem,
76
+ char, mbstate_t>`.
77
+
78
+ An object of this class template stores:
79
+
80
+ - `byte_err_string` — a byte string to display on errors
81
+ - `wide_err_string` — a wide string to display on errors
82
+ - `cvtptr` — a pointer to the allocated conversion object (which is
83
+ freed when the `wstring_convert` object is destroyed)
84
+ - `cvtstate` — a conversion state object
85
+ - `cvtcount` — a conversion count
86
+
87
+ ``` cpp
88
+ using byte_string = basic_string<char, char_traits<char>, Byte_alloc>;
89
+ ```
90
+
91
+ The type shall be a synonym for
92
+ `basic_string<char, char_traits<char>, Byte_alloc>`.
93
+
94
+ ``` cpp
95
+ size_t converted() const noexcept;
96
+ ```
97
+
98
+ *Returns:* `cvtcount`.
99
+
100
+ ``` cpp
101
+ wide_string from_bytes(char byte);
102
+ wide_string from_bytes(const char* ptr);
103
+ wide_string from_bytes(const byte_string& str);
104
+ wide_string from_bytes(const char* first, const char* last);
105
+ ```
106
+
107
+ *Effects:* The first member function shall convert the single-element
108
+ sequence `byte` to a wide string. The second member function shall
109
+ convert the null-terminated sequence beginning at `ptr` to a wide
110
+ string. The third member function shall convert the sequence stored in
111
+ `str` to a wide string. The fourth member function shall convert the
112
+ sequence defined by the range \[`first`, `last`) to a wide string.
113
+
114
+ In all cases:
115
+
116
+ - If the `cvtstate` object was not constructed with an explicit value,
117
+ it shall be set to its default value (the initial conversion state)
118
+ before the conversion begins. Otherwise it shall be left unchanged.
119
+ - The number of input elements successfully converted shall be stored in
120
+ `cvtcount`.
121
+
122
+ *Returns:* If no conversion error occurs, the member function shall
123
+ return the converted wide string. Otherwise, if the object was
124
+ constructed with a wide-error string, the member function shall return
125
+ the wide-error string. Otherwise, the member function throws an object
126
+ of class `range_error`.
127
+
128
+ ``` cpp
129
+ using int_type = typename wide_string::traits_type::int_type;
130
+ ```
131
+
132
+ The type shall be a synonym for `wide_string::traits_type::int_type`.
133
+
134
+ ``` cpp
135
+ state_type state() const;
136
+ ```
137
+
138
+ returns `cvtstate`.
139
+
140
+ ``` cpp
141
+ using state_type = typename Codecvt::state_type;
142
+ ```
143
+
144
+ The type shall be a synonym for `Codecvt::state_type`.
145
+
146
+ ``` cpp
147
+ byte_string to_bytes(Elem wchar);
148
+ byte_string to_bytes(const Elem* wptr);
149
+ byte_string to_bytes(const wide_string& wstr);
150
+ byte_string to_bytes(const Elem* first, const Elem* last);
151
+ ```
152
+
153
+ *Effects:* The first member function shall convert the single-element
154
+ sequence `wchar` to a byte string. The second member function shall
155
+ convert the null-terminated sequence beginning at `wptr` to a byte
156
+ string. The third member function shall convert the sequence stored in
157
+ `wstr` to a byte string. The fourth member function shall convert the
158
+ sequence defined by the range \[`first`, `last`) to a byte string.
159
+
160
+ In all cases:
161
+
162
+ - If the `cvtstate` object was not constructed with an explicit value,
163
+ it shall be set to its default value (the initial conversion state)
164
+ before the conversion begins. Otherwise it shall be left unchanged.
165
+ - The number of input elements successfully converted shall be stored in
166
+ `cvtcount`.
167
+
168
+ *Returns:* If no conversion error occurs, the member function shall
169
+ return the converted byte string. Otherwise, if the object was
170
+ constructed with a byte-error string, the member function shall return
171
+ the byte-error string. Otherwise, the member function shall throw an
172
+ object of class `range_error`.
173
+
174
+ ``` cpp
175
+ using wide_string = basic_string<Elem, char_traits<Elem>, Wide_alloc>;
176
+ ```
177
+
178
+ The type shall be a synonym for
179
+ `basic_string<Elem, char_traits<Elem>, Wide_alloc>`.
180
+
181
+ ``` cpp
182
+ explicit wstring_convert(Codecvt* pcvt = new Codecvt);
183
+ wstring_convert(Codecvt* pcvt, state_type state);
184
+ explicit wstring_convert(const byte_string& byte_err,
185
+ const wide_string& wide_err = wide_string());
186
+ ```
187
+
188
+ *Requires:* For the first and second constructors, `pcvt != nullptr`.
189
+
190
+ *Effects:* The first constructor shall store `pcvt` in `cvtptr` and
191
+ default values in `cvtstate`, `byte_err_string`, and `wide_err_string`.
192
+ The second constructor shall store `pcvt` in `cvtptr`, `state` in
193
+ `cvtstate`, and default values in `byte_err_string` and
194
+ `wide_err_string`; moreover the stored state shall be retained between
195
+ calls to `from_bytes` and `to_bytes`. The third constructor shall store
196
+ `new Codecvt` in `cvtptr`, `state_type()` in `cvtstate`, `byte_err` in
197
+ `byte_err_string`, and `wide_err` in `wide_err_string`.
198
+
199
+ ``` cpp
200
+ ~wstring_convert();
201
+ ```
202
+
203
+ *Effects:* The destructor shall delete `cvtptr`.
204
+