<html>
<head>
<title>New Character Types in C++</title>
</head>

<body>
<h1>New Character Types in C++</h1>

<p>ISO/IEC JTC1 SC22 WG21 N2149 = 07-0009 - 2007-01-10

<p>Lawrence Crowl

<p>This document replaces N2018 = 06-0088 - 2006-06-20.

<h2>Problem</h2>

<p>Many users of C++ need to manipulate Unicode character strings.
Unfortunately, there is no C++ standard means to do so.

<h2>Solution</h2>

<p>The
<a href="http://www.open-std.org/jtc1/sc22/wg14/">ISO C</a>
committee has addressed this issue extensively.
See
<a href="http://www.iso.org/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=33907&ICS1=35&ICS2=60&ICS3=&scopelist=">
ISO/IEC TR 19769:2004</a>
"Extensions for the programming language C
to support new character data types"
as described in draft report ISO/IEC JTC1 SC22 WG14
<a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1040.pdf">
N1040</a>
at
<a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1040.pdf">
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1040.pdf</a>.

<p>This proposal adopts their work,
but with those changes necessary for effective use within C++.
In particular, we propose new types to support overloading.

<p>A separate proposal will address specializations for
numeric_limits, character traits, basic strings,
streams, and insertion operations.

<h2>References</h2>

<p>See section 2.5 "Encoding Forms" in
<blockquote>
The Unicode Consortium.
The Unicode Standard, Version 5.0.0, defined by:
<cite>The Unicode Standard, Version 5.0</cite>
(Boston, MA, Addison-Wesley, 2007. ISBN 0-321-48091-0)
</blockquote>
The online version (printing prohibited)
is at
<a href="http://www.unicode.org/versions/Unicode5.0.0/">
http://www.unicode.org/versions/Unicode5.0.0/</a>.

<p>See
<a href="http://www.dkuug.dk/JTC1/SC2/WG2/docs/n2005/n2005-2.doc">
Annex C</a>
of
<a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/projects#10646">ISO
10646</a>-1,
which is online at
<a href="http://www.dkuug.dk/JTC1/SC2/WG2/docs/n2005/n2005-2.doc">
http://www.dkuug.dk/JTC1/SC2/WG2/docs/n2005/n2005-2.doc</a>.

<p>See
<a href="http://www.iso.org/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=39921&ICS1=35&ICS2=40&ICS3=">ISO/IEC 10646:2003</a>,
which is publicly available
in several text and PDF files within a zip archive from 
<a href="http://standards.iso.org/ittf/PubliclyAvailableStandards/c039921_ISO_IEC_10646_2003%28E%29.zip">
http://standards.iso.org/ittf/PubliclyAvailableStandards/c039921_ISO_IEC_10646_2003%28E%29.zip</a>.

<p>See
<a href="http://www.unicode.org/faq/utf_bom.html">UTF-8,
UTF-16, UTF-32 &amp; BOM</a>.

<h2>Summary of ISO/IEC TR 19769 (WG14 N1040)</h2>

<p>The document ISO/IEC TR 19769 (WG14 N1040) provides motivation,
new typedefs for the (at least) 16-bit and
(at least) 32-bit character types,
macros for reporting
<a href="http://std.dkuug.dk/JTC1/SC2/WG2/docs/projects#10646">
ISO 10646</a>
encoding,
character and string literals, mixed string concatenation,
four library functions,
and a new header with appropriate declarations.

<h2>Summary of Changes to the C Proposal for C++</h2>

<p>The document ISO/IEC TR 19769 (WG14 N1040) can be adopted with few changes.
Further changes are possible,                                               
but this proposal minimizes the changes to ensure maximum interoperability.

<h3>Define new primitive types.</h3>

<p>Define <tt>char16_t</tt> to be a distinct new type,
that has the same size and representation as <tt>uint_least16_t</tt>.
Likewise,
define <tt>char32_t</tt> to be a distinct new type,
that has the same size and representation as <tt>uint_least32_t</tt>.

<p>[N1040 defined <tt>char16_t</tt> and <tt>char32_t</tt>
as typedefs to <tt>uint_least16_t</tt> and <tt>uint_least32_t</tt>,
which make overloading on these characters impossible.]

<p>[The experiments on open-source software
indicate that these identifiers are not commonly used,
and when used,
used in a manner consistent with the proposal.]

<h3>Add C++-specific headers.</h3>

<p>Add a new C++ header <tt>&lt;cuchar&gt;</tt>
corresponding to the new C header <tt>&lt;uchar.h&gt;</tt>.

<h3>Clarify literals.</h3>

<p>Clarify the handling of universal character names
that do not fit with <tt>char16_t</tt>.
In particular, the interaction with ISO 10646 UTF-16                          
is underspecified in the C proposal.                                         

<h3>Require UTF.</h3>

<p>The C TR makes the encoding of <tt>char16_t</tt> and <tt>char32_t</tt>
implementation-defined.
It also provides macros to indicate whether or not the encoding is UTF.
In contrast, this proposal requires UTF encoding.

<h2>Changes to the C++ Standard</h2>

<h3>2.11 Keywords</h3>

<p>To "Table 3 -- keywords",
add <tt>char16_t</tt> and <tt>char32_t</tt>.

<h3>2.13.2 Character literals</h3>

<p>To the grammar, add
<blockquote>
<dl>
<dt><var>character-literal:</var></dt>
<dd><tt>u'</tt> <var>c-char-sequence</var> <tt>'</tt></dd>
<dd><tt>U'</tt> <var>c-char-sequence</var> <tt>'</tt></dd>
</dl>
</blockquote>

<p>To paragraph 1, replace
<blockquote>
optionally preceded by the letter <tt>L</tt>, as in <tt>L'x'</tt>
</blockquote>
with
<blockquote>
optionally preceded by one of the letters
<tt>L</tt>, <tt>u</tt>, or <tt>U</tt>,
as in <tt>L'x'</tt>, <tt>u'y'</tt>, <tt>U'z'</tt>, respectively
</blockquote>

<p>To paragraph 2, add
<blockquote>
A character literal that begins with the letter <tt>u</tt>,
such as <tt>u'y'</tt>,
is a character literal of type <tt>char16_t</tt>.
The value of a <tt>char16_t</tt> literal
containing a single <var>c-char</var>
is equal to its ISO 10646 code point value,
provided that the code point is representable with a single 16-bit code unit.
(That is, provided it is a basic multi-lingual plane code point.)
If the value is not representable within 16 bits,
the program is ill-formed.
A <tt>char16_t</tt> literal containing multiple <var>c-char</var>s           
is ill-formed.                                                                
It is implementation-defined whether literals
may contain more than members of the basic character set
and universal character names
(<tt>\U</tt><var>nnnnnnnn</var> and <tt>\u</tt><var>nnnn</var>).
</blockquote>

<p>To paragraph 2, add
<blockquote>
A character literal that begins with the letter <tt>U</tt>,
such as <tt>U'z'</tt>,
is a character literal of type <tt>char32_t</tt>.
The value of a <tt>char32_t</tt> literal
containing a single <var>c-char</var>
is equal to its ISO 10646 code point value.
A <tt>char32_t</tt> literal containing multiple <var>c-char</var>s           
is ill-formed.                                                                
It is implementation-defined whether literals
may contain more than members of the basic character set
and universal character names
(<tt>\U</tt><var>nnnnnnnn</var> and <tt>\u</tt><var>nnnn</var>).
</blockquote>

<p>In paragraph 4, replace
<blockquote>
range defined for
<tt>char</tt> (for ordinary literals)
or <tt>wchar_t</tt> (for wide literals)
</blockquote>
with
<blockquote>
range defined for
<tt>char</tt> (for ordinary literals),
<tt>char16_t</tt> (for at-least-16-bit literals),
<tt>char32_t</tt> (for at-least-32-bit literals),
or <tt>wchar_t</tt> (for wide literals)
</blockquote>

<h3>2.13.4 String literals</h3>

<p>To the grammar, add
<blockquote>
<dl>
<dt><var>string-literal:</var></dt>
<dd><tt>u"</tt> <var>c-char-sequence<sub>opt</sub></var> <tt>"</tt></dd>
<dd><tt>U"</tt> <var>c-char-sequence<sub>opt</sub></var> <tt>"</tt></dd>
</dl>
</blockquote>

<p>To paragraph 1, replace
<blockquote>
optionally beginning with the letter <tt>L</tt>,
as in <tt>"..."</tt> or <tt>L"..."</tt>
</blockquote>
with
<blockquote>
optionally beginning with one of the letters
<tt>L</tt>, <tt>u</tt>, or <tt>U</tt>,
as in <tt>"..."</tt>, <tt>L"..."</tt>,
<tt>u"..."</tt>, or <tt>U"..."</tt>,
respectively
</blockquote>

<p>To paragraph 1, append
<blockquote>
A string literal that begins with <tt>u</tt>, such as <tt>u"asdf"</tt>,
is a <tt>char16_t</tt> string literal.
A <tt>char16_t</tt> string literal has type
<q>array of <var>n</var> <tt>const char16_t</tt></q>
and has static storage duration,
where <var>n</var> is the size of the string as defined below,
and is initialized with the given characters.
It is implementation-defined whether literals
may contain more than members of the basic character set
and universal character names
(<tt>\U</tt><var>nnnnnnnn</var> and <tt>\u</tt><var>nnnn</var>).
A single <tt><var>c-char</var></tt>
may produce more than one <tt>char16_t</tt>
in the form of surrogate pairs.

A string literal that begins with <tt>U</tt>, such as <tt>U"asdf"</tt>,
is a <tt>char32_t</tt> string literal.
A <tt>char32_t</tt> string literal has type
<q>array of <var>n</var> <tt>const char32_t</tt></q>
and has static storage duration,
where <var>n</var> is the size of the string as defined below,
and is initialized with the given characters.
It is implementation-defined whether literals
may contain more than members of the basic character set
and universal character names
(<tt>\U</tt><var>nnnnnnnn</var> and <tt>\u</tt><var>nnnn</var>).
</blockquote>

<p>In paragraph 3, replace
<blockquote>
In translation phase 6 (2.1),
adjacent narrow string literals are concatenated
and adjacent wide string literals are concatenated.
If a narrow string literal token
is adjacent to a wide string literal token,
the behavior is undefined.
</blockquote>
with
<blockquote>
In translation phase 6 (2.1),
adjacent string literals are concatenated.
If both string literals have the same prefix,
the resulting concatenated string literal has that prefix.
If one string literal has no prefix,
it is treated as a string literal
of the same prefix as the other operand.
Any other concatenations have conditionally supported behavior.
Note that this concatentation is an interpretation, not a conversion.
[Example: Here are some examples of valid concatenations:
<table border=1 cellpadding=2>
<tr>
<th>source</th><th>means</th>
<th>source</th><th>means</th>
<th>source</th><th>means</th>
<tr>
<td><tt>u"a" u"b"</tt></td><td><tt>u"ab"</tt></td>
<td><tt>U"a" U"b"</tt></td><td><tt>U"ab"</tt></td>
<td><tt>L"a" L"b"</tt></td><td><tt>L"ab"</tt></td>
<tr>
<td><tt>u"a" "b"</tt></td><td><tt>u"ab"</tt></td>
<td><tt>U"a" "b"</tt></td><td><tt>U"ab"</tt></td>
<td><tt>L"a" "b"</tt></td><td><tt>L"ab"</tt></td>
<tr>
<td><tt>"a" u"b"</tt></td><td><tt>u"ab"</tt></td>
<td><tt>"a" U"b"</tt></td><td><tt>U"ab"</tt></td>
<td><tt>"a" L"b"</tt></td><td><tt>L"ab"</tt></td>
</table>
]
</blockquote>

<p>In paragraph 5, replace

<blockquote>
The size of a wide string literal
is the total number of escape sequences,
universal-character-names, and other characters,
plus one for the terminating <tt>L'\0'</tt>.
</blockquote>

with

<blockquote>
The length of a wide or <tt>char32_t</tt> string literal
is the total number of escape sequences,
universal-character-names, and other characters,
plus one for the terminating <tt>L'\0'</tt> or <tt>U'\0'</tt>.
The size of such a literal is the
size of the base character type times the length.
The universal-character-names must be representable
by the type of the literal.
The length of a <tt>char16_t</tt> string literal
is the total number of escape sequences,
universal-character-names, and other characters,
plus one for each character requiring a surrogate pair,
plus one for the terminating <tt>u'\0'</tt>.
[Note: The length of a <tt>char16_t</tt> string literal
is the number of code units,
not the number of characters.]
The size of such a literal is the
size of <tt>char16_t</tt> times the length.
The universal-character-names must be within the range 0x0 to 0x10FFFF.
</blockquote>

<h3>3.9.1 Fundamental Types</h3>

<p>At the end of paragraph 5, add
<blockquote>
Types <tt>char16_t</tt> and <tt>char32_t</tt>
denote distinct types with the same size, signedness, and alignment as
<tt>uint_least16_t</tt> and <tt>uint_least32_t</tt>,
respectively, in <tt>&lt;stdint.h&gt;</tt>,
called the underlying types.
</blockquote>

<p>The <tt>&lt;stdint.h&gt;</tt> header is from                               
<a href="http://www.open-std.org/jtc1/sc22/wg14/">ISO C</a>                   
as proposed in document WG21 N1835 = 05-0095,                                
and subsequently adopted into                                               
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf">
ISO/IEC TR 19768: C++ Library Extensions TR1</a>.                          

<p>In paragraph 7, append
<blockquote>
<tt>char16_t</tt>, <tt>char32_t</tt>,
</blockquote>
to
<blockquote>
<tt>wchar_t</tt>,
</blockquote>

<h3>4.2 Array-to-pointer conversion</h3>

<p>In paragraph 2, replace
<blockquote>
A string literal (2.13.4)
that is not a wide string literal
can be converted to an rvalue of type
<q>pointer to <tt>char</tt></q>;
a wide string literal can be converted to an rvalue of type
<q>pointer to <tt>wchar_t</tt></q>.
In either case
</blockquote>
with
<blockquote>
A string literal (2.13.4)
with no prefix, <tt>L</tt> prefix, <tt>u</tt> prefix,
or <tt>U</tt> prefix
can be converted to an rvalue of type
<q>pointer to <tt>char</tt></q>,
<q>pointer to <tt>wchar_t</tt></q>,
<q>pointer to <tt>char16_t</tt></q>,
or <q>pointer to <tt>char32_t</tt></q>,
respectively.
In any case
</blockquote>

<h3>4.5 Integral promotions</h3>

<p>In paragraph 2, append
<blockquote>
, <tt>char16_t</tt>, or <tt>char32_t</tt>,
</blockquote>
to
<blockquote>
<tt>wchar_t</tt>
</blockquote>

<h3>5 Expressions</h3>

<p>In footnote 54, append
<blockquote>
<tt>char16_t</tt>, <tt>char32_t</tt>,
</blockquote>
to
<blockquote>
<tt>wchar_t</tt>,
</blockquote>

<h3>5.3.3 Sizeof</h3>

<p>In paragraph 1, replace
<blockquote>
and <tt>sizeof(wchar_t)</tt>,
</blockquote>
with
<blockquote>
<tt>sizeof(wchar_t)</tt>,
<tt>sizeof(char16_t)</tt>,
and <tt>sizeof(char32_t)</tt>,
</blockquote>

<h3>7.1.5.2 Simple type specifiers</h3>

<p>To the grammar, add
<blockquote>
<dl>
<dt><var>simple-type-specifier:</var></dt>
<dd><tt>char16_t</tt></dd>
<dd><tt>char32_t</tt></dd>
</dl>
</blockquote>

<p>To Table 7, add
<blockquote>
<table>
<tr><td><tt>char16_t</tt></td><td><q><tt>char16_t</tt></q></td></tr>
<tr><td><tt>char32_t</tt></td><td><q><tt>char32_t</tt></q></td></tr>
</table>
</blockquote>

<h3>8.5 Initializers</h3>

<p>In paragraph 15, bullet 2, replace
<blockquote>
or an array of <tt>wchar_t</tt>,
</blockquote>
with
<blockquote>
an array of <tt>wchar_t</tt>,
an array of <tt>char16_t</tt>,
or an array of <tt>char32_t</tt>,
</blockquote>

<h3>8.5.2 Character arrays</h3>

<p>In paragraph 1, replace
<blockquote>
A <tt>char</tt> array
(whether plain <tt>char</tt>, <tt>signed char</tt>,
or <tt>unsigned char</tt>)
can be initialized by a <var>string-literal</var>
(optionally enclosed in braces);
a <tt>wchar_t</tt> array
can be initialized by a wide <var>string-literal</var>
(optionally enclosed in braces);
successive characters of the <var>string-literal</var>
initialize the members of the array.
</blockquote>
with
<blockquote>
A <tt>char</tt> array
(whether plain <tt>char</tt>, <tt>signed char</tt>,
or <tt>unsigned char</tt>),
<tt>wchar_t</tt> array,
<tt>char16_t</tt> array,
or <tt>char32_t</tt> array
can be initialized by a <var>string-literal</var>
(optionally enclosed in braces)
with no prefix,
with <tt>L</tt> prefix,
with <tt>u</tt> prefix, or
with <tt>U</tt> prefix, respectively.
Successive characters of the <var>string-literal</var>
initialize the members of the array.
</blockquote>

<h3>15.1 Throwing an exception</h3>

<p>In paragraph 3, replace
<blockquote>
<tt>char*</tt> or <tt>wchar_t*</tt>
</blockquote>
with
<blockquote>
<tt>char*</tt>, <tt>char16_t*</tt>, <tt>char32_t*</tt>,
or <tt>wchar_t*</tt>
</blockquote>
Replace
<blockquote>
array of <tt>const char</tt> and array of <tt>const wchar_t</tt>
</blockquote>
with
<blockquote>
array of <tt>const char</tt>, array of <tt>const char16_t</tt>,
array of <tt>const char32_t</tt>, or array of <tt>const wchar_t</tt>
</blockquote>
Replace
<blockquote>
pointer to <tt>char</tt> or pointer to <tt>wchar_t</tt>
</blockquote>
with
<blockquote>
pointer to <tt>char</tt>, pointer to <tt>char16_t</tt>,
pointer to <tt>char32_t</tt>, or pointer to <tt>wchar_t</tt>
</blockquote>

<h3>17 Library introduction</h3>

<p>In paragraph 4, replace
<blockquote>
sequences of type <tt>wchar_t</tt>,
</blockquote>
with
<blockquote>
sequences of type <tt>wchar_t</tt>,
sequences of type <tt>char16_t</tt>,
sequences of type <tt>char32_t</tt>,
</blockquote>

<h3>17.1.2 character</h3>

<p>In paragraph 1, replace
<blockquote>
and <tt>wchar_t</tt>,
</blockquote>
with
<blockquote>
<tt>wchar_t</tt>,
<tt>char16_t</tt>,
and <tt>char32_t</tt>,
</blockquote>

<h3>17.3.2.1.3.4 [NEW] char16-character sequences</h3>

<p>A <dfn>char16-character sequence</dfn>
is an array object (8.3.4) <tt>A</tt>
that can be declared as <tt>T A[N]</tt>,
where <tt>T</tt> is type <tt>char16_t</tt> (3.9.1),
optionally qualified
by any combination of <tt>const</tt> and <tt>volatile</tt>.
The initial elements of the array have defined contents
up to and including an element determined by some predicate.
A character sequence can be designated by a pointer value <tt>S</tt>
that designates its first element.

<p>A <dfn>null-terminated char16-character string</dfn>,
or <dfn>NTC16S</dfn>,
is a char16-character sequence
whose highest-addressed element with defined content
has the value zero.
[Footnote: Many of the objects
manipulated by function signatures declared in <tt>&lt;cuchar&gt;</tt>
are char16-character sequences or NTC16Ss.]

<p>The <dfn>length of an NTC16S</dfn>
is the number of elements
that precede the terminating null char16 character.
An <dfn>empty NTC16S</dfn> has a length of zero.

<p>The <dfn>value of an NTC16S</dfn>
is the sequence of values of the elements
up to and including the terminating null character.

<p>A <dfn>static NTC16S</dfn>
is an NTC16S with static storage duration.
[Footnote: A char16 string literal, such as <tt>u"abc"</tt>,
is a static NTC16S.]

<h3>17.3.2.1.3.5 [NEW] char32-character sequences</h3>

<p>A <dfn>char32-character sequence</dfn>
is an array object (8.3.4) <tt>A</tt>
that can be declared as <tt>T A[N]</tt>,
where <tt>T</tt> is type <tt>char32_t</tt> (3.9.1),
optionally qualified
by any combination of <tt>const</tt> and <tt>volatile</tt>.
The initial elements of the array have defined contents
up to and including an element determined by some predicate.
A character sequence can be designated by a pointer value <tt>S</tt>
that designates its first element.

<p>A <dfn>null-terminated char32-character string</dfn>,
or <dfn>NTC32S</dfn>,
is a char32-character sequence
whose highest-addressed element with defined content
has the value zero.
[Footnote: Many of the objects
manipulated by function signatures declared in <tt>&lt;cuchar&gt;</tt>
are char32-character sequences or NTC32Ss.]

<p>The <dfn>length of an NTC32S</dfn>
is the number of elements
that precede the terminating null char32 character.
An <dfn>empty NTC32S</dfn> has a length of zero.

<p>The <dfn>value of an NTC32S</dfn>
is the sequence of values of the elements
up to and including the terminating null character.

<p>A <dfn>static NTC32S</dfn>
is an NTC32S with static storage duration.
[Footnote: A char32 string literal, such as <tt>U"abc"</tt>,
is a static NTC32S.]

<h3>17.4.1.2 Headers</h3>

<p>To table 12, add <tt>&lt;cuchar&gt;</tt>.

<h3>17.4.3.1.3 External linkage</h3>

<p>In paragraph 5, footnote 168, add <tt>&lt;cuchar&gt;</tt>.

<h3>21.4 Null-terminated sequence utilities</h3>

<p>Add paragraph 20,

<blockquote>
<p>Table 50 describes headers
<tt>&lt;cuchar&gt;</tt> and <tt>&lt;uchar.h&gt;</tt>.
The distinction is that <tt>&lt;cuchar&gt;</tt>
defines the function and typedef names within namespace <tt>std</tt>
and that <tt>&lt;uchar.h&gt;</tt>
defines them at global scope.
</blockquote>

<p>Add Table 50,
<blockquote>
<table border=1 cellpadding=2>
<caption>Table 50 -- Headers <tt>&lt;cuchar&gt;</tt> and
<tt>&lt;uchar.h&gt;</tt> synopsis</caption>
<tr><th colspan=2>Typedef Names</th></tr>
<tr><td>char16_t</td><td>char32_t</td></tr>
<tr><th colspan=2>Macro Names</th></tr>
<tr><td>__STDC_UTF_16__</td><td>__STDC_UTF_32__</td></tr>
<tr><th colspan=2>Function Names</th></tr>
<tr><td>mbrtoc16</td><td>c16rtomb</td></tr>
<tr><td>mbrtoc32</td><td>c32rtomb</td></tr>
</table>
</blockquote>

<h3>21 Strings Library</h3>

<p>Add <tt>&lt;cuchar&gt;</tt> to table 38
under "Null-terminated sequence utilities".

<h3>21.5 [NEW] char16 and char32 characters</h2>

<p>The headers <tt>&lt;cuchar&gt;</tt> and <tt>&lt;uchar.h&gt;</tt>
define macros and declare functions
for use with at-least-16-bit and at-least-32-bit characters.

<h3>21.5.1 [NEW] The <tt>__STDC_UTF_16__</tt>
and <tt>__STDC_UTF_32__</tt> macros</h3>

<p>The headers <tt>&lt;cuchar&gt;</tt> and <tt>&lt;uchar.h&gt;</tt>
define the macro <tt>__STDC_UTF_16__</tt>,
and values of type <tt>char16_t</tt> shall be valid UTF-16 code units,
as defined by ISO 10646.
</dl>

<p>The headers <tt>&lt;cuchar&gt;</tt> and <tt>&lt;uchar.h&gt;</tt>
shall define the macro <tt>__STDC_UTF_32__</tt>,
and values of type <tt>char32_t</tt> shall be valid UTF-32 code units,
as defined by ISO 10646.
</dl>

<h3>21.5.2 [NEW] The <tt>mbrtoc16</tt> function</h3>

<h4>Synopsis</h4>

<blockquote><tt>
#include &lt;cuchar&gt;<br>
size_t std::mbrtoc16(char16_t * pc16, const char * s, size_t n, mbstate_t * ps);
</tt></blockquote>

<h4>Description</h4>

<p>If <tt>s</tt> is a null pointer,
the <tt>mbrtoc16</tt> function is equivalent to the call:
<blockquote>
<tt>mbrtoc16(NULL, "", 1, ps)</tt>
</blockquote>
In this case,
the values of the parameters <tt>pc16</tt> and <tt>n</tt>
are ignored.

<p>If s is not a null pointer,
the <tt>mbrtoc16</tt> function inspects at most <tt>n</tt> bytes
beginning with the byte pointed to by <tt>s</tt>
to determine the number of bytes needed
to complete the next multibyte character
(including any shift sequences).
If the function determines
that the next multibyte character is complete and valid,
it determines the value of the corresponding wide character
and then, if <tt>pc16</tt> is not a null pointer,
stores that value in the object pointed to by <tt>pc16</tt>.
If the corresponding wide character is the null wide character,
the resulting state described is the initial conversion state.

<h4>Returns</h4>

The <tt>mbrtoc16</tt> function
returns the first of the following that applies
(given the current conversion state):

<dl>

<dt><tt>0</tt></dt>
<dd>if the next <tt>n</tt> or fewer bytes
complete the multibyte character
that corresponds to the null wide character
(which is the value stored).</dd>

<dt><tt>[1..n]</tt></dt>
<dd>if the next <tt>n</tt> or fewer bytes
complete a valid multibyte character
(which is the value stored);
the value returned is the number of bytes
that complete the multibyte character.</dd>

<dt><tt>(size_t)(-3)</tt></dt>
<dd>if the multibyte sequence
converted more than one corresponding <tt>char32_t</tt> character
and not all these characters have yet been stored;
the next character in the sequence has now been stored
and no bytes from the input have been consumed by this call.</dd>

<dt><tt>(size_t)(-2)</tt></dt>
<dd>if the next <tt>n</tt> bytes
contribute to an incomplete (but potentially valid) multibyte character,
and all n bytes have been processed (no value is stored).

<p>Note:
When <tt>n</tt> has at least the value of the <tt>MB_CUR_MAX</tt> macro,
this case can only occur
if <tt>s</tt> points at a sequence of redundant shift sequences
(for implementations with state-dependent encodings).</dd>

<dt><tt>(size_t)(-1)</tt></dt>
<dd>if an encoding error occurs,
in which case the next <tt>n</tt> or fewer bytes
do not contribute to a complete and valid multibyte character
(no value is stored);
the value of the macro <tt>EILSEQ</tt>
is stored in <tt>errno</tt>,
and the conversion state is unspecified.</dd>

</dl>

<h3>21.5.3 [NEW] The <tt>c16rtomb</tt> function</h3>

<h4>Synopsis</h4>

<blockquote><tt>
#include &lt;cuchar&gt;<br>
size_t std::c16rtomb(char * s, char16_t c16, mbstate _t * ps);
</tt></blockquote>

<h4>Description</h4>

<p>If <tt>s</tt> is a null pointer,
the <tt>c16rtomb</tt> function is equivalent to the call
<blockquote>
<tt>c16rtomb(buf, L'\0', ps)</tt>
</blockquote>
where <tt>buf</tt> is an internal buffer.

<p>If <tt>s</tt> is not a null pointer,
the <tt>c16rtomb</tt> function
determines the number of bytes needed
to represent the multibyte character
that corresponds to the wide character given by <tt>c16</tt>
(including any shift sequences),
and stores the multibyte character representation
in the array whose first element is pointed to by <tt>s</tt>.
At most <tt>MB_CUR_MAX</tt> bytes are stored.
If <tt>c16</tt> is a null wide character,
a null byte is stored,
preceded by any shift sequence
needed to restore the initial shift state;
the resulting state described is the initial conversion state.

<h4>Returns</h4>

<p>The <tt>c16rtomb </tt>function
returns the number of bytes stored in the array object;
this may be 0 (including any shift sequences).
When <tt>c16</tt> is not a valid wide character,
an encoding error occurs:
the function
stores the value of the macro <tt>EILSEQ</tt> in <tt>errno</tt>
and returns <tt>(size_t)(-1)</tt>;
the conversion state is unspecified.

<h3>21.5.4 [NEW] The <tt>mbrtoc32</tt> function</h3>

<h4>Synopsis</h4>

<blockquote><tt>
#include &lt;cuchar&gt;<br>
size_t std::mbrtoc32(char32_t * pc32, const char * s, size_t n, mbstate_t * ps);
</tt></blockquote>

<h4>Description</h4>

<p>If <tt>s</tt> is a null pointer,
the <tt>mbrtoc32</tt> function is equivalent to the call:
<blockquote>
<tt>mbrtoc32(NULL, "", 1, ps)</tt>
</blockquote>
In this case,
the values of the parameters <tt>pc32</tt> and <tt>n</tt> are ignored.

<p>If <tt>s</tt> is not a null pointer,
the <tt>mbrtoc32</tt> function inspects at most <tt>n</tt> bytes
beginning with the byte pointed to by <tt>s</tt>
to determine the number of bytes needed
to complete the next multibyte character
(including any shift sequences).
If the function determines that the next multibyte character
is complete and valid,
it determines the value of the corresponding wide character
and then, if <tt>pc32</tt> is not a null pointer,
stores that value in the object pointed to by <tt>pc32</tt>.
If the corresponding wide character is the null wide character,
the resulting state described is the initial conversion state.

<h4>Returns</h4>

The <tt>mbrtoc32</tt> function
returns the first of the following that applies
(given the current conversion state):

<dl>

<dt><tt>0</tt></dt>
<dd>if the next <tt>n</tt> or fewer bytes
complete the multibyte character
that corresponds to the null wide character
(which is the value stored).</dd>

<dt><tt>[1..n]</tt></dt>
<dd>if the next <tt>n</tt> or fewer bytes
complete a valid multibyte character (which is the value stored);
the value returned is the number of bytes
that complete the multibyte character.</dd>

<dt><tt>(size_t)(-3)</tt></dt>
<dd>if the multibyte sequence
converted more than one corresponding <tt>char32_t</tt> character
and not all these characters have yet been stored;
the next character in the sequence has now been stored
and no bytes from the input have been consumed by this call.</dd>

<dt><tt>(size_t)(-2)</tt></dt>
<dd>if the next <tt>n</tt> bytes
contribute to an incomplete (but potentially valid) multibyte character,
and all n bytes have been processed (no value is stored).

<p>Note:
When <tt>n</tt> has at least the value of the <tt>MB_CUR_MAX</tt> macro,
this case can only occur
if <tt>s</tt> points at a sequence of redundant shift sequences
(for implementations with state-dependent encodings).</dd>

<dt><tt>(size_t)(-1)</tt></dt>
<dd>if an encoding error occurs,
in which case the next <tt>n</tt> or fewer bytes
do not contribute to a complete and valid multibyte character
(no value is stored);
the value of the macro <tt>EILSEQ</tt> is stored in <tt>errno</tt>,
and the conversion state is unspecified.</dd>

</dl>

<h3>21.5.5 [NEW] The <tt>c32rtomb</tt> function</h3>

<h4>Synopsis</h4>

<blockquote><tt>
#include &lt;cuchar&gt;<br>
size_t std::c32rtomb(char * s, char32_t c32, mbstate_t * ps);
</tt></blockquote>

<h4>Description</h4>

<p>If <tt>s</tt> is a null pointer,
the <tt>c32rtomb</tt> function is equivalent to the call
<blockquote>
<tt>c32rtomb(buf, L'\0', ps)</tt>
</blockquote>
where <tt>buf </tt>is an internal buffer.

<p>If <tt>s</tt> is not a null pointer,
the <tt>c32rtomb</tt> function
determines the number of bytes needed
to represent the multibyte character
that corresponds to the wide character given by <tt>c32</tt>
(including any shift sequences),
and stores the multibyte character representation
in the array whose first element is pointed to by <tt>s</tt>.
At most <tt>MB_CUR_MAX</tt> bytes are stored.
If <tt>c32</tt> is a null wide character,
a null byte is stored,
preceded by any shift sequence
needed to restore the initial shift state;
the resulting state described is the initial conversion state.

<h4>Returns</h4>

<p> The <tt>c32rtomb</tt> function
returns the number of bytes stored in the array object;
this may be 0 (including any shift sequences).
When <tt>c32</tt> is not a valid wide character,
an encoding error occurs:
the function
stores the value of the macro <tt>EILSEQ</tt> in <tt>errno</tt>
and returns <tt>(size_t)(-1)</tt>;
the conversion state is unspecified.

<h3>A.6 Declarations</h3>

<p>To the grammar, add
<blockquote>
<dl>
<dt><var>simple-type-specifier:</var></dt>
<dd><tt>char16_t</tt></dd>
<dd><tt>char32_t</tt></dd>
</dl>
</blockquote>

<h3>C.1.1 Clause 2: lexical conventions</h3>

<p>At the end of Subclause _lex.string: Change:, add
<blockquote>
The type of a char16 string literal is changed from
array of <var>some-integer-type</var>
to array of const <tt>char16_t</tt>.
The type of a char32 string literal is changed from
array of <var>some-integer-type</var>
to array of const <tt>char32_t</tt>.
</blockquote>

<h3>C.2.2.4 Header &lt;uchar.h&gt;</h3>

<p>Add section.
<blockquote>
The typedefs <tt>char16_t</tt> and <tt>char32_t</tt>
are distinct types
rather than typedefs to existing integral types.
</blockquote>

<h3>D.5 Standard C Library Headers</h3>

<p>Replace "18 C headers"
with "18 C headers and 1 C technical report header".

<p>To table 101, add
<blockquote>
<tt>&lt;uchar.h&gt;</tt>
</blockquote>

</body>
</html>
