<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to StrCvt</title><link>https://sourceforge.net/p/xprintf/wiki/StrCvt/</link><description>Recent changes to StrCvt</description><atom:link href="https://sourceforge.net/p/xprintf/wiki/StrCvt/feed" rel="self"/><language>en</language><lastBuildDate>Wed, 02 Jul 2014 09:46:01 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/xprintf/wiki/StrCvt/feed" rel="self" type="application/rss+xml"/><item><title>StrCvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/StrCvt/</link><description>&lt;div class="markdown_content"&gt;&lt;h1 id="strcvt-character-type-converter-for-c11"&gt;StrCvt - Character type converter for C++11&lt;/h1&gt;
&lt;h2 id="contents"&gt;Contents&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="" href="#Introduction"&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Installation"&gt;Installation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Usage"&gt;Usage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#string_conversion"&gt;String conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#U8String"&gt;UTF-8 string type &lt;code&gt;u8string&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#utf_string_conversion"&gt;UTF string conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#locale_dependent"&gt;Locale dependent stream conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#utf_stream_conversion"&gt;UTF stream conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#converting_iterator"&gt;Character type converting iterator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#operators"&gt;Operators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Library"&gt;Creating a Library&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Compatibility"&gt;Compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Rationale"&gt;Rationale&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#License"&gt;License&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="introduction-wzxhzdk15wzxhzdk16"&gt;Introduction &lt;a name="Introduction"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++11 has the new character types &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;, which use&lt;br /&gt;
the UTF-16 and UTF-32 Unicode encoding. The question is how to convert&lt;br /&gt;
between the four character types of C++.&lt;/p&gt;
&lt;p&gt;This library converts between the different character types of C++.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Conversion between the types &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; in all combinations.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Implements the API of the C++ &lt;code&gt;codecvt&lt;/code&gt; member functions in() and&lt;br /&gt;
  out() for stream conversion, and convenience functions for string&lt;br /&gt;
  conversion.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Locale-dependent conversion and UTF conversion.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="installation-wzxhzdk17wzxhzdk18"&gt;Installation &lt;a name="Installation"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Unpack the distribution to a directory on your local machine.  You can&lt;br /&gt;
include the headers in subdirectory &lt;code&gt;include/xprintf&lt;/code&gt; from your&lt;br /&gt;
program.  To make inclusion of the headers easier, it is recommended to&lt;br /&gt;
add the subdirectory &lt;code&gt;include&lt;/code&gt; of the distribution to the include file&lt;br /&gt;
search path of the compiler.  This is commonly achieved with the&lt;br /&gt;
option &lt;code&gt;-I/path/to/strcvt/include&lt;/code&gt; (assuming that the xprintf&lt;br /&gt;
distribution has been upacked to directory &lt;code&gt;/path/to/xprintf&lt;/code&gt;).  Then&lt;br /&gt;
you can include the code converter headers through their standard names&lt;br /&gt;
like "strcvt/strcvt.h".&lt;/p&gt;
&lt;h2 id="usage-wzxhzdk19wzxhzdk20"&gt;Usage &lt;a name="Usage"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In order to use the code converter, its headers have to be included.&lt;br /&gt;
They are:&lt;br /&gt;
- &lt;a class="" href="#U8String"&gt;UTF-8 string type &lt;code&gt;u8string&lt;/code&gt;&lt;/a&gt;&lt;br /&gt;
~~~cpp&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// The main header defining the strcvt() string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The main header defining the u8string type&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/u8string.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_utf() UTF string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the charcvt() stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/charcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the charcvt_utf() UTF stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/charcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_iterator&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt_iterator.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining codecvt_utf&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/codecvt_utf.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Optional header defining output operators&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt_operators.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Import symbols into users namespace&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;If the xprintf include directory &lt;code&gt;/path/to/strcvt/include&lt;/code&gt; has been&lt;br /&gt;
added to the include file search path of the compiler, e.g. using the&lt;br /&gt;
compiler option &lt;code&gt;-I/path/to/strcvt/include&lt;/code&gt;, this reduces to:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// The main header defining the strcvt() string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The main header defining the u8string type&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/u8string.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_utf() UTF string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the charcvt() stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/charcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the charcvt_utf() UTF stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/charcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_iterator&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt_iterator.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining codecvt_utf&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/codecvt_utf.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Optional header defining output operators&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt_operators.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Import symbols into users namespace&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The strcvt() functions are exported through namespace &lt;code&gt;StrCvt&lt;/code&gt;.  It&lt;br /&gt;
is recommented to make them available via a &lt;code&gt;using namespace&lt;/code&gt;&lt;br /&gt;
directive like in the example above.  Alternatively it is possible to&lt;br /&gt;
import the functions strcvt() and strcvt_utf() etc. separately&lt;br /&gt;
through &lt;code&gt;using&lt;/code&gt; declarations:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;strcvt_utf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;strcvt_utf_strict&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_strcvt_iterator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The character type converter is pre-configured for header-only use.&lt;br /&gt;
This means: Just include the header and you are done.  In order to&lt;br /&gt;
reduce space overhead and compilation time, a precompiled library can&lt;br /&gt;
be used.  See section &lt;a class="" href="#library"&gt;Creating a Library&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="string-conversion-wzxhzdk21wzxhzdk22"&gt;String conversion &lt;a name="string_conversion"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Function strcvt\&amp;lt;dest_string_type&gt;(source) converts strings from the&lt;br /&gt;
source character type to the destination string type.  The&lt;br /&gt;
&lt;code&gt;dest_string_type&lt;/code&gt; can be &lt;code&gt;std::string&lt;/code&gt;, &lt;code&gt;std::wstring&lt;/code&gt;,&lt;br /&gt;
&lt;code&gt;std::u16string&lt;/code&gt; and &lt;code&gt;std::u32string&lt;/code&gt;. An additional string type&lt;br /&gt;
&lt;code&gt;StrCvt::u8string&lt;/code&gt; with UTF-8 encoding is also provided; see section&lt;br /&gt;
&lt;a class="" href="#U8String"&gt;UTF-8 string type &lt;code&gt;u8string&lt;/code&gt;&lt;/a&gt;. Example usage:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;quot;strcvt/strcvt.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Convert const char* C string to wide wchar_t string&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;wstring&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;wstring&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Hello, world!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Convert wide string to string of different character type&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Convert string to char16_t string&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;u16string&lt;/span&gt; &lt;span class="n"&gt;s16&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;u16string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// The source buffer can also be specified as (pointer, size):&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s16&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The source string can be specified as:&lt;br /&gt;
- a character pointer of any character type (C-style null-terminated string),&lt;br /&gt;
- a C++ string of arbitrary character type, or&lt;br /&gt;
- a character pointer and a size.&lt;/p&gt;
&lt;p&gt;The destination string type is specified as a template argument to&lt;br /&gt;
strcvt().  The function is specialized on all combinations of source&lt;br /&gt;
and destination character types. So the full signatures are:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;size_t&lt;/span&gt; &lt;span class="nb"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The function with string argument &lt;code&gt;dest_string
strcvt&amp;lt;dest_string&amp;gt;(source_string source)&lt;/code&gt; is overloaded for the case&lt;br /&gt;
&lt;code&gt;dest_string&lt;/code&gt; == &lt;code&gt;source_string&lt;/code&gt;: it moves its argument to the result&lt;br /&gt;
(for rvalue arguments) or returns a reference to its argument (for&lt;br /&gt;
lvalue arguments).  This is useful in generic code where the concrete&lt;br /&gt;
types are not known.&lt;/p&gt;
&lt;p&gt;It is also possible to omit the template argument determining the&lt;br /&gt;
destination string type.  In this case, strcvt() does no conversion at&lt;br /&gt;
all, but returns an object which can be converted to all string types.&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// strcvt() returns an object which can be assigned to any string type&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;wstring&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Hello, world!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;u16string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;For string arguments, the return value of strcvt() is a subclass of&lt;br /&gt;
the class of its argument.  The subclass adds conversion operators to&lt;br /&gt;
all string types.  For character pointer arguments, the return value&lt;br /&gt;
of strcvt() is an object which can be converted to a character&lt;br /&gt;
pointer (of the character type of the argument) or to any string type.&lt;br /&gt;
In each case, the return value of strcvt() can e.g. be assigned to&lt;br /&gt;
any string type, or can be passed as argument to a function expecting a&lt;br /&gt;
string.&lt;/p&gt;
&lt;h2 id="utf-8-string-type-u8stringwzxhzdk23wzxhzdk24"&gt;UTF-8 string type &lt;code&gt;u8string&lt;/code&gt;&lt;a name="U8String"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In addition to the four standard string types &lt;code&gt;std::string&lt;/code&gt;,&lt;br /&gt;
&lt;code&gt;std::wstring&lt;/code&gt;, std::u16string&lt;code&gt;and&lt;/code&gt;std::u32string&lt;code&gt;provided by the
C++ standard library, header strcvt/u8string.h defines an additional
string type&lt;/code&gt;StrCvt::u8string&lt;code&gt;. Like the locale dependent string type&lt;/code&gt;std::string&lt;code&gt;it has character type&lt;/code&gt;char`, but it always uses a&lt;br /&gt;
locale-independent UTF-8 encoding.  Example usage:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;quot;strcvt/u8string.h&amp;quot;&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;quot;strcvt/strcvt.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;u8string&lt;/span&gt; &lt;span class="nf"&gt;us&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u8&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Hello, World&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;us&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;In the example above, the &lt;code&gt;u8string&lt;/code&gt; is initialized using the C++11&lt;br /&gt;
UTF-8 character literal &lt;code&gt;u8"string"&lt;/code&gt;.  For output to the&lt;br /&gt;
locale-dependent standard output stream, it is converted from UTF-8 to&lt;br /&gt;
the locale dependent &lt;code&gt;char&lt;/code&gt; endoding.&lt;/p&gt;
&lt;h2 id="utf-string-conversion-wzxhzdk25wzxhzdk26"&gt;UTF string conversion &lt;a name="utf_string_conversion"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In the examples above, strcvt() can be replaced with strcvt_utf(),&lt;br /&gt;
which ignores the locale and always performs a UTF transformation.&lt;br /&gt;
The header is "strcvt/strcvt_utf.h".&lt;/p&gt;
&lt;p&gt;It is also possible to configure strcvt() to only use UTF conversions&lt;br /&gt;
instead of the locale dependent character conversions.  Open the header&lt;br /&gt;
&lt;code&gt;strcvt/strcvt_config.h&lt;/code&gt; with an editor and change the preprocessor&lt;br /&gt;
symbol &lt;code&gt;STRCVT_IMPL_UTF8_ONLY&lt;/code&gt; from 0 to 1.&lt;/p&gt;
&lt;h2 id="locale-dependent-stream-conversion-wzxhzdk27wzxhzdk28"&gt;Locale-dependent stream conversion &lt;a name="locale_dependent"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The character type stream converter function charcvt(state, from,&lt;br /&gt;
from_end, from_next, to, to_end, to_next) is defined in header&lt;br /&gt;
strcvt/charcvt.h and uses the API of the member functions in()&lt;br /&gt;
and out() of the standard code conversion interface &lt;code&gt;std::codecvt&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="vi"&gt;#include&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;strcvt/charcvt.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="nx"&gt;using&lt;/span&gt; &lt;span class="nx"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;mbstate_t&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;mbstate_t&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Zero-initialize&lt;/span&gt;
&lt;span class="c1"&gt;// Convert between character types:&lt;/span&gt;
&lt;span class="nb"&gt;result&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;charcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;from_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;from_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ... More calls of charcvt()&lt;/span&gt;

&lt;span class="c1"&gt;// Return output to initial shift state&lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;charcvt_unshift&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;state&lt;/code&gt; must be of type &lt;code&gt;std::mbstate_t&lt;/code&gt;.  It holds the conversion&lt;br /&gt;
state between successive calls of charcvt() and must be explicitly&lt;br /&gt;
zero-initialized before the first use, like shown above.  The&lt;br /&gt;
arguments &lt;code&gt;from&lt;/code&gt; and &lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer,&lt;br /&gt;
&lt;code&gt;to&lt;/code&gt; and &lt;code&gt;to_end&lt;/code&gt; delimit the destination character buffer, and on&lt;br /&gt;
exit from the function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last&lt;br /&gt;
converted character.  The function returns &lt;code&gt;std::codecvt_base::ok&lt;/code&gt; on&lt;br /&gt;
success and &lt;code&gt;std::codecvt_base::partial&lt;/code&gt; if the output buffer was too&lt;br /&gt;
small to convert the entire input buffer, or if the input buffer ended&lt;br /&gt;
in a part of a multibyte sequence.  After the input has been&lt;br /&gt;
completely converted, possibly by multiple calls to charcvt(),&lt;br /&gt;
charcvt_unshift() must be called to move the output to the initial&lt;br /&gt;
shift state.  The source character type must be specified as a&lt;br /&gt;
template argument to charcvt_unshift(), since different converters are&lt;br /&gt;
used depending on the source character type.  Charcvt_unshift() should&lt;br /&gt;
even be used if the character encoding is known not to be state&lt;br /&gt;
dependent, like UTF-8.  Charcvt_unshift() checks whether trailing&lt;br /&gt;
incomplete Unicode character input sequences are pending, and appends&lt;br /&gt;
a replacement character to the output buffer if necessary to signal&lt;br /&gt;
the presence of trailing garbage.&lt;/p&gt;
&lt;p&gt;Charcvt() is specialized for all combinations of source and&lt;br /&gt;
destination character types &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; and converts between the locale-dependent character type&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt;, the Unicode UTF-16 and UTF-32 character types &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt;, and the implementation defined wide character type&lt;br /&gt;
&lt;code&gt;wchar_t&lt;/code&gt; which is assumed to be equivalent to either &lt;code&gt;char16_t&lt;/code&gt; or&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt;.  It is able to consume or deliver single characters from&lt;br /&gt;
multi-character Unicode characters.&lt;/p&gt;
&lt;p&gt;The full signature of charcvt() is:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;codecvt_base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="n"&gt;charcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;mbstate_t&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;from_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="o"&gt;*&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;from_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;*&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;dst_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;flags&lt;/code&gt; can be omitted, or they can be set to the constant&lt;br /&gt;
&lt;code&gt;strcvt_flags_no_partial_conversions&lt;/code&gt; to prevent partial conversions.&lt;br /&gt;
If the flag is omitted or zero, charcvt() is eager and consumes even&lt;br /&gt;
single partial multibyte characters.&lt;/p&gt;
&lt;h2 id="utf-stream-conversion-wzxhzdk29wzxhzdk30"&gt;UTF stream conversion &lt;a name="utf_stream_conversion"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The function charcvt_utf(state, from, from_end, from_next, to, to_end,&lt;br /&gt;
to_next) is defined in header strcvt/charcvt_utf.h and works like&lt;br /&gt;
charcvt() but treats the type &lt;code&gt;char&lt;/code&gt; as having UTF-8 encoding instead&lt;br /&gt;
of a locale dependent implementation defined encoding:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;quot;strcvt/charcvt_utf.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;mbstate_t&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;mbstate_t&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Zero-initialize&lt;/span&gt;
&lt;span class="c1"&gt;// Convert between character types:&lt;/span&gt;
&lt;span class="n"&gt;charcvt_utf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ... More calls of charcvt_utf()&lt;/span&gt;

&lt;span class="c1"&gt;// Return output to initial shift state&lt;/span&gt;
&lt;span class="n"&gt;charcvt_utf_unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;state&lt;/code&gt; must be of type &lt;code&gt;std::mbstate_t&lt;/code&gt;.  It holds the conversion&lt;br /&gt;
state between successive calls of charcvt_utf() and must be explicitly&lt;br /&gt;
zero-initialized before the first use, like shown above.  The&lt;br /&gt;
arguments &lt;code&gt;from&lt;/code&gt; and &lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer,&lt;br /&gt;
&lt;code&gt;to&lt;/code&gt; and &lt;code&gt;to_end&lt;/code&gt; delimit the destination character buffer, and on&lt;br /&gt;
exit from the function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last&lt;br /&gt;
converted character.  The function returns &lt;code&gt;std::codecvt_base::ok&lt;/code&gt; on&lt;br /&gt;
success and &lt;code&gt;std::codecvt_base::partial&lt;/code&gt; if the output buffer was too&lt;br /&gt;
small to convert the entire input buffer, or if the input buffer ended&lt;br /&gt;
in a part of a multibyte sequence.  After the input has been&lt;br /&gt;
completely converted, possibly by multiple calls to charcvt_utf(),&lt;br /&gt;
charcvt_utf_unshift() checks whether trailing incomplete Unicode&lt;br /&gt;
character input sequences are pending, and appends a replacement&lt;br /&gt;
character to the output buffer if necessary to signal the presence of&lt;br /&gt;
trailing garbage.&lt;/p&gt;
&lt;p&gt;Charcvt_utf() is specialized for all combinations of source and&lt;br /&gt;
destination character types &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; and converts between UTF-coded &lt;code&gt;char&lt;/code&gt;, the Unicode UTF-16&lt;br /&gt;
and UTF-32 character types &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;, and the&lt;br /&gt;
implementation defined wide character type &lt;code&gt;wchar_t&lt;/code&gt; which is assumed&lt;br /&gt;
to be equivalent to either &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  Contrary to&lt;br /&gt;
member functions in() and out() of &lt;code&gt;codecvt&lt;/code&gt;, charcvt_utf() always&lt;br /&gt;
converts (even if the input character type is the same as the output&lt;br /&gt;
character type, in which case it checks the validity of the encoding).&lt;br /&gt;
It always generates valid UTF-8, UTF-16 or UTF-32 output sequences.&lt;/p&gt;
&lt;p&gt;For each invalid encoding in the input buffer, charcvt_utf() inserts&lt;br /&gt;
the replacement character U+0xFFFD into the output buffer.  If this is&lt;br /&gt;
not wanted, function charcvt_utf_strict() can be used, which has the&lt;br /&gt;
same interface as charcvt_utf() but returns &lt;code&gt;std::codecvt_base::error&lt;/code&gt;&lt;br /&gt;
on encoding errors, with &lt;code&gt;from_next&lt;/code&gt; pointing to the first element of&lt;br /&gt;
the invalid sequence.&lt;/p&gt;
&lt;p&gt;There is also a UTF code conversion facet defined in header&lt;br /&gt;
strcvt/codecvt_utf.h&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;codecvt_utf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;intern_charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extern_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;This code conversion facet uses charcvt_utf() to convert between UTF&lt;br /&gt;
coded characters &lt;code&gt;intern_charT&lt;/code&gt; and &lt;code&gt;extern_charT&lt;/code&gt;, and is specialized&lt;br /&gt;
for all character type combinations.&lt;/p&gt;
&lt;h2 id="character-type-converting-character-iterator-wzxhzdk31wzxhzdk32"&gt;Character type converting character iterator &lt;a name="converting_iterator"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Instead of converting the entire buffer, it can be accessed trough a&lt;br /&gt;
converting character iterator.  The iterator is created by function&lt;br /&gt;
make_strcvt_iterator():&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="vi"&gt;#include&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;strcvt/strcvt_iterator.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="nx"&gt;using&lt;/span&gt; &lt;span class="nx"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Create char32_t iterator for access to source C string&lt;/span&gt;
&lt;span class="nx"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;make_strcvt_iterator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;char32_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Hello, world!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The end iterator is returned by make_strcvt_iterator() without&lt;br /&gt;
arguments, or it can be obtained from member function end() of the&lt;br /&gt;
iterator.  Since member function begin() is also implemented, and&lt;br /&gt;
returns the iterator itself, the iterator can be used just like a&lt;br /&gt;
container, for example with the range-based for statement:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;u32string&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Use range-based for statement: process all Unicode characters&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char32_t&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Alternatively, use simple iterator interface&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The converting iterator is (indirectly) derived from base class&lt;br /&gt;
&lt;code&gt;strcvt_iterator_base&amp;lt;charT&amp;gt;&lt;/code&gt; which does not depend on the source&lt;br /&gt;
iterator type or the source character type. It has a virtual&lt;br /&gt;
destructor and can be used polymorphically.  The inheritance hierarchy&lt;br /&gt;
looks like this:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;strcvt_iterator_base&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Operators *(), ++(), ==()&lt;/span&gt;
    &lt;span class="c1"&gt;// Member functions begin(), end()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;strcvt_iterator_impl&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;strcvt_iterator_base&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Implements operator ++() which does the conversion using&lt;/span&gt;
    &lt;span class="c1"&gt;// virtual member function get_next_source_char()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;SourceIterator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;strcvt_iterator&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;strcvt_iterator_impl&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iterator_traits&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SourceIterator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;value_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Implements get_next_source_char()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;Class template &lt;code&gt;strcvt_iterator_base&lt;/code&gt; does not know the source&lt;br /&gt;
character type and can be used to handle a &lt;code&gt;strcvt_iterator&lt;/code&gt;&lt;br /&gt;
polymorphically.  Class template &lt;code&gt;strcvt_iterator_impl&lt;/code&gt; knows the&lt;br /&gt;
source character type but not the type of the iterator.  If the&lt;br /&gt;
precompiled library is used, it is precompiled for all combinations of&lt;br /&gt;
source and destination character type.  Class template&lt;br /&gt;
&lt;code&gt;strcvt_iterator&lt;/code&gt; is the return value of function&lt;br /&gt;
make_strcvt_iterator().  It implements the source iterator handling.&lt;/p&gt;
&lt;h2 id="operators-wzxhzdk33wzxhzdk34"&gt;Operators &lt;a name="operators"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Some operators for strings are defined in header&lt;br /&gt;
"strcvt/strcvt_operators.h".  These are the output operators &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; for&lt;br /&gt;
strings and character pointers, and the appending &lt;code&gt;+=&lt;/code&gt; operators for&lt;br /&gt;
strings.  To use them, include header "strcvt/strcvt_operators.h".&lt;br /&gt;
The operators reside in namespace StrCvt and should be imported into&lt;br /&gt;
the user's namespace through a &lt;code&gt;using namespace&lt;/code&gt; directive:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;quot;strcvt/strcvt_operators.h&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;U&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Hello, world!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;It would be nice if we could define conversion operators to enable&lt;br /&gt;
assignment between different string types.  But in C++, conversion and&lt;br /&gt;
assignment operators can only be defined as member functions.&lt;/p&gt;
&lt;h2 id="creating-a-library-wzxhzdk35wzxhzdk36"&gt;Creating a Library &lt;a name="Library"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The character type converter is preconfigured for header-only use.&lt;br /&gt;
This means: Just include the header and you are done.  In order to&lt;br /&gt;
reduce space overhead and compilation time, a precompiled library can&lt;br /&gt;
be used.&lt;/p&gt;
&lt;p&gt;The main advantage in using a library is that each time the strcvt&lt;br /&gt;
headers are included, the compiler does not need to look at the&lt;br /&gt;
implementation details.  This can speed up compilation significantly.&lt;/p&gt;
&lt;p&gt;To create the library, the C++ source files libstrcvt.cpp,&lt;br /&gt;
libstrcvt_iterator.cpp, libstrcvt_utf.cpp and libcodecvt_utf.cpp in&lt;br /&gt;
directory &lt;code&gt;lib&lt;/code&gt; of the distribution must be compiled.  Under Linux,&lt;br /&gt;
just run &lt;code&gt;make&lt;/code&gt;.  Before compiling, you may want to select the&lt;br /&gt;
compiler to use: Uncomment to proper &lt;em&gt;CXX=&lt;/em&gt; - line in the toplevel&lt;br /&gt;
&lt;code&gt;Makefile.template&lt;/code&gt;.  Running &lt;code&gt;make&lt;/code&gt; should create a library&lt;br /&gt;
&lt;code&gt;lib/libxprintf.a&lt;/code&gt;, which has to be linked to the programs.&lt;/p&gt;
&lt;p&gt;In Visual C++, instead of building a library, you may just add the&lt;br /&gt;
library source files to your project.&lt;/p&gt;
&lt;p&gt;In order to make the headers use the library, you must open the header&lt;br /&gt;
&lt;code&gt;strcvt/strcvt_config.h&lt;/code&gt; with an editor and change the preprocessor&lt;br /&gt;
symbol &lt;code&gt;STRCVT_IMPL_USE_LIBRARY&lt;/code&gt; from 0 to 1.  The next time the&lt;br /&gt;
header is included, the library will be used.  You can check that the&lt;br /&gt;
library is used as intended by omitting the library when linking.&lt;br /&gt;
Linking should fail with missing externals.&lt;/p&gt;
&lt;p&gt;In order to run the tests, the headers for the &lt;em&gt;boost&lt;/em&gt; test framework&lt;br /&gt;
are required.&lt;/p&gt;
&lt;h2 id="compatibilitywzxhzdk37wzxhzdk38"&gt;Compatibility&lt;a name="Compatibility"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The code converter has been tested with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GCC 4.7, 4.8 and 4.9 on Linux&lt;/li&gt;
&lt;li&gt;Visual Studio Express 2013 for Windows Desktop with November 2013 CTP&lt;/li&gt;
&lt;li&gt;Intel C++ 14.0.2 on Linux&lt;/li&gt;
&lt;li&gt;Clang 3.5.0 on Linux&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="rationale-wzxhzdk39wzxhzdk40"&gt;Rationale &lt;a name="Rationale"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++11 has the new character types &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;, which use&lt;br /&gt;
the UTF-16 and UTF-32 Unicode encoding. The question is how to convert&lt;br /&gt;
between the four character types of C++.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;codecvt&lt;/code&gt; part of the C++11 library looks like some ruins left&lt;br /&gt;
over at the front line between warring factions.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;codecvt&lt;/code&gt; class template is part of the header \&amp;lt;locale&gt; and&lt;br /&gt;
described in section 22.4.1.4 of the standard.  The first thing to&lt;br /&gt;
note is that according to this specification &lt;code&gt;codecvt&lt;/code&gt; transforms&lt;br /&gt;
between an &lt;em&gt;internal&lt;/em&gt; and an &lt;em&gt;external&lt;/em&gt; character encoding, so it is&lt;br /&gt;
not intended to transform between internal character encodings like&lt;br /&gt;
&lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;wchar_t&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;According to the standard, each locale shall have specializations of&lt;br /&gt;
&lt;code&gt;codecvt&lt;/code&gt; for tranformation between the internal character types&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt; and the external&lt;br /&gt;
character type &lt;code&gt;char&lt;/code&gt;.  The standard first says that these&lt;br /&gt;
specializations "convert the implementation-defined native character&lt;br /&gt;
set", only to continue specifying that&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;the specialization with external and internal character type both&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt; must not convert at all (effectively saying that the internal&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt; must have the same encoding as the external &lt;code&gt;char&lt;/code&gt;),&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;the specializations with internal character types &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; must treat the external character code as UTF-8, so they&lt;br /&gt;
  are explicitly not allowed to treat it as an "implementation-defined&lt;br /&gt;
  native character set".&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This leaves the transformation between internal character type&lt;br /&gt;
&lt;code&gt;wchar_t&lt;/code&gt; and external character type &lt;code&gt;char&lt;/code&gt; as the only&lt;br /&gt;
locale-dependent transformation, and it is specified to convert&lt;br /&gt;
"between the native character sets for narrow and wide characters".&lt;br /&gt;
So no luck using &lt;code&gt;codecvt&lt;/code&gt; to transform between native character sets&lt;br /&gt;
and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Section 22.5 of the standard specifies the "standard code conversion&lt;br /&gt;
facets".  It contains:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Facet &lt;code&gt;codecvt_utf8&lt;/code&gt;, which converts between a UTF-8 coded &lt;code&gt;char&lt;/code&gt;&lt;br /&gt;
  buffer and UCS2 or UCS4, so it can be used to transform between&lt;br /&gt;
  UTF-8 and &lt;code&gt;char32_t&lt;/code&gt;. It is not usable for transformation between&lt;br /&gt;
  UTF-8 and &lt;code&gt;char16_t&lt;/code&gt; because &lt;code&gt;char16_t&lt;/code&gt; strings are coded as&lt;br /&gt;
  UTF-16, not UCS2.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Facet &lt;code&gt;codecvt_utf8_utf16&lt;/code&gt; converts between UTF-8 and UTF-16, so it&lt;br /&gt;
  can be used to transform between UTF-8-coded &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Facet &lt;code&gt;codecvt_utf16&lt;/code&gt; looks like a hack from the 20th century to&lt;br /&gt;
  adapt UTF-16 wide characters to character byte streams.  The&lt;br /&gt;
  UTF-16-coded buffer is addressed through a &lt;code&gt;char*&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So we can use these standard code conversion facets to convert between&lt;br /&gt;
UTF-8-coded &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  We could even&lt;br /&gt;
transform between &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt; by going through an&lt;br /&gt;
intermediate UTF-8-coded &lt;code&gt;char&lt;/code&gt; buffer.  But again, no&lt;br /&gt;
transformation between native character sets and &lt;code&gt;char16_t&lt;/code&gt; or&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The standard does provide an interface for transformation between&lt;br /&gt;
native &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  It is hidden at the very&lt;br /&gt;
end of section 21.7 in the string library and consists of the four&lt;br /&gt;
functions mbrtoc16(), c16rtomb(), mbrtoc32() and c32rtomb().&lt;br /&gt;
Unfortunately, a locale can not be specified for these functions.&lt;br /&gt;
They always work with the currently active global locale.&lt;/p&gt;
&lt;p&gt;For the following things I know of no standard-conforming procedure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Conversion between &lt;code&gt;wchar_t&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  Clearly,&lt;br /&gt;
  going through native &lt;code&gt;char&lt;/code&gt; is not an option, unless it happens to&lt;br /&gt;
  use UTF-8 encoding.  I worked around this by assuming that the native&lt;br /&gt;
&lt;code&gt;wchar_t&lt;/code&gt; type uses Unicode coding and is equivalent to either&lt;br /&gt;
&lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;, depending on &lt;code&gt;sizeof(wchar_t)&lt;/code&gt;.  This&lt;br /&gt;
  assumption holds for the systems I have access to, but there may be&lt;br /&gt;
  other &lt;code&gt;wchar_t&lt;/code&gt; encodings in use.  Older Windows systems used UCS2,&lt;br /&gt;
  where this assumption would not hold, but modern versions of Windows&lt;br /&gt;
  use UTF-16.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Locale-dependent conversion: especially for output streams where a&lt;br /&gt;
  locale is known that may not be the global locale, it would be&lt;br /&gt;
  useful if the locale could be passed as an argument to the character&lt;br /&gt;
  transformation.  Unfortunately the standard library does not provide&lt;br /&gt;
  a portable way to do this, so I left it out.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First I thought I would reuse the standard-specified UTF coders, but&lt;br /&gt;
first they are incomplete (going from UTF-16 to UTF-32 via an&lt;br /&gt;
intermediate representation as UTF-8 is not very attractive), second&lt;br /&gt;
they are distributed over several different interfaces, and third GCC&lt;br /&gt;
does not have them.  So as a first step I implemented a transformation&lt;br /&gt;
between UTF-coded &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt;, &lt;code&gt;char32_t&lt;/code&gt; and &lt;code&gt;wchar_t&lt;/code&gt;.  The&lt;br /&gt;
function charcvt_utf(state, from, from_end, from_next, to, to_end,&lt;br /&gt;
to_next) uses the API of the member functions in() and out() of&lt;br /&gt;
&lt;code&gt;codecvt&lt;/code&gt;.  The &lt;code&gt;state&lt;/code&gt; holds the conversion state, &lt;code&gt;from&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer, &lt;code&gt;to&lt;/code&gt; and &lt;code&gt;to_end&lt;/code&gt;&lt;br /&gt;
delimit the destination character buffer, and on exit from the&lt;br /&gt;
function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last converted&lt;br /&gt;
character.  Like in() and out(), charcvt_utf() returns&lt;br /&gt;
&lt;code&gt;std::codecvt_base::ok&lt;/code&gt; on success and &lt;code&gt;std::codecvt_base::partial&lt;/code&gt; if&lt;br /&gt;
the output buffer was too small to convert the entire input buffer, or&lt;br /&gt;
if the input buffer ended in a part of a multibyte sequence.  For each&lt;br /&gt;
invalid encoding in the input buffer, the replacement character&lt;br /&gt;
U+0xFFFD is inserted into the output stream.  If this is not wanted,&lt;br /&gt;
function charcvt_utf_strict() can be used, which has the same&lt;br /&gt;
interface as charcvt_utf() but returns &lt;code&gt;std::codecvt_base::error&lt;/code&gt; on&lt;br /&gt;
encoding errors, with &lt;code&gt;from_next&lt;/code&gt; pointing to the first element of the&lt;br /&gt;
invalid sequence.  Both functions are specialized on all combinations&lt;br /&gt;
of &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;.  They always convert&lt;br /&gt;
(even if input and output type is the same, in which case they check&lt;br /&gt;
the validity of the encoding).  They are able to consume and produce&lt;br /&gt;
single characters forming part of multibyte Unicode characters.&lt;/p&gt;
&lt;p&gt;Based on the UTF coder, a locale-dependent converter has been&lt;br /&gt;
implemented. The function charcvt(state, from, from_end, from_next,&lt;br /&gt;
to, to_end, to_next) also uses the API of the member functions in()&lt;br /&gt;
and out() of &lt;code&gt;codecvt&lt;/code&gt;:  The &lt;code&gt;state&lt;/code&gt; holds the conversion state,&lt;br /&gt;
&lt;code&gt;from&lt;/code&gt; and &lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer, &lt;code&gt;to&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;to_end&lt;/code&gt; delimit the destination character buffer, and on exit from&lt;br /&gt;
the function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last converted&lt;br /&gt;
character.  This implementation uses the assumption that &lt;code&gt;wchar_t&lt;/code&gt; is&lt;br /&gt;
encoded as either UTF-16 or UTF-32.  Transformations between wide&lt;br /&gt;
characters &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt; use UTF&lt;br /&gt;
transformations, transformations between &lt;code&gt;char&lt;/code&gt; and wide characters&lt;br /&gt;
treat &lt;code&gt;char&lt;/code&gt; as a native locale-defined character code.&lt;/p&gt;
&lt;p&gt;Convenience functions for the code transformation are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;std::basic_string strcvt&amp;gt;(const C*&lt;br /&gt;
  from, std::size_t size) converts the &lt;code&gt;size&lt;/code&gt; characters at &lt;code&gt;from&lt;/code&gt;&lt;br /&gt;
  into a string of character type &lt;code&gt;charT&lt;/code&gt;, which is specified as a&lt;br /&gt;
  template parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;std::basic_string strcvt&amp;gt;(const C*&lt;br /&gt;
  from) converts the characters of the null-terminated string &lt;code&gt;from&lt;/code&gt;&lt;br /&gt;
  into a string of character type &lt;code&gt;charT&lt;/code&gt;, which is specified as a&lt;br /&gt;
  template parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;std::basic_string strcvt&amp;gt;(const&lt;br /&gt;
  std::basic_string&amp;amp; from) converts the characters of the string&lt;br /&gt;
&lt;code&gt;from&lt;/code&gt; into a string of character type &lt;code&gt;charT&lt;/code&gt;, which is specified&lt;br /&gt;
  as a template parameter.  Specializations for &lt;code&gt;charT == C&lt;/code&gt; move the&lt;br /&gt;
  string argument to the return value (if the argument is an rvalue)&lt;br /&gt;
  or return the reference to the argument (if the argument is an&lt;br /&gt;
  lvalue).  This is useful in generic code where the concrete types&lt;br /&gt;
  are not known.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;strcvt(const C* from) (strcvt() without template parameter&lt;br /&gt;
  specifying the destination string type) creates an object which&lt;br /&gt;
  can be converted back to a &lt;code&gt;const C*&lt;/code&gt; or to any string type.  The&lt;br /&gt;
  result can e.g. be assigned to any string type, or passed to a&lt;br /&gt;
  function expecting any string.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;strcvt(const std::basic_string&amp;amp; from) (strcvt() without template&lt;br /&gt;
  parameter specifying the destination string type) casts the type of&lt;br /&gt;
  the argument to a reference to a class derived from&lt;br /&gt;
  std::basic_string, which adds conversion operators to all string&lt;br /&gt;
  types.  The result can e.g. be assigned to any string type, or&lt;br /&gt;
  passed to a function expecting any string.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="license-wzxhzdk41wzxhzdk42"&gt;License &lt;a name="License"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Copyright (c) 2014 Ruediger Helsch; All rights reserved&lt;/p&gt;
&lt;p&gt;Permission to use, copy, modify, and distribute this software for any&lt;br /&gt;
purpose and without fee is hereby granted.  The author disclaims all&lt;br /&gt;
warranties with regard to this software.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Wed, 02 Jul 2014 09:46:01 -0000</pubDate><guid>https://sourceforge.net17d45df314141273eec83e6c3f9cea94431546f2</guid></item></channel></rss>