Read UTF-8 file into UCS-4 string
<p>I am trying to read a UTF-8 encoded file into a UTF-32 (UCS-4) string. Basically internally I want a fixed size character internally to the application.</p>

<p>Here I want to make sure the translation is done as part of the stream processes (because that is what the Locale is supposed to be used for). Alternative questions have been posted to do the translation on the string (but this is wasteful as you have to do a translation phase in memory then you have to do a second pass to send it to the stream). By doing it with the locale in the stream you only have to do a single pass and there is not requirement for a copy to made (assuming you want to maintain the original).</p>

<p>This is what I tried.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;locale&gt;
#include &lt;codecvt&gt;

int main()
{
    std::locale     converter(std::locale(), new std::codecvt_utf8&lt;char32_t&gt;);
    std::basic_ifstream&lt;char32_t&gt;   iFile;
    iFile.imbue(converter);
    iFile.open("test.data");

    std::u32string     line;
    while(std::getline(iFile, line))
    {
    }
}
</code></pre>

<p>Since thes are all standard types I was surprized with this compilation error:</p>

<pre><code>/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/istream:275:41:
error: no matching function for call to 'use_facet'

            const ctype&lt;_CharT&gt;&amp; __ct = use_facet&lt;ctype&lt;_CharT&gt; &gt;(__is.getloc());
                                        ^~~~~~~~~~~~~~~~~~~~~~~~~
</code></pre>

<p>Compiled with:</p>

<pre><code>g++ -std=c++14 test.cpp
</code></pre>