Implementing mbsnrtowcs in terms of mbsrtowcs
<p>In an almost complete effort to port libc++ to Windows, I need the function <code>mbsnrtowcs</code>, and thought that the easiest way would be to implement it in terms of <code>mbsrtowcs</code>:</p>

<pre><code>size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
               size_t nmc, size_t len, mbstate_t *__restrict__ ps )
{
    char* nmcsrc = new char[nmc+1];
    strncpy( nmcsrc, *src, nmc );
    nmcsrc[nmc] = '\0';
    const size_t result = mbsrtowcs( dst, &amp;nmcsrc, len, ps );
    delete[] nmcsrc;
    return result;
}
</code></pre>

<p>The problem here is that <code>mbsrtowcs</code> needs <code>&amp;nmcsrc</code> to be of type <code>const char**</code>, which is isn't, because it is a string I just copied the first <code>nmc</code> elements into, and appended a <code>\0</code> character to. How can I work around this? Strictly speaking, this is compiled as C++, so perhaps a <code>const_cast</code> would do little harm here? I also have access to c++0X (libc++ is/requires a subset c++0x).</p>

<p>Edit: the Clang error message reads:</p>

<pre><code>M:\Development\Source\libc++\src\support\win32\support.cpp:37:27: error: no matching function for call to 'mbsrtowcs'
const size_t result = mbsrtowcs( dst, &amp;nmcsrc, len, ps );
                      ^~~~~~~~~
M:/Development/mingw64/bin/../lib/clang/3.0/../../../x86_64-w64-mingw32/include\wchar.h:732:18: note: candidate function not viable: no known conversion from 'char **' to 'const char **restrict' for 2nd argument;
  size_t __cdecl mbsrtowcs(wchar_t * __restrict__ _Dest,const char ** __restrict__ _PSrc,size_t _Count,mbstate_t * __restrict__ _State) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
                 ^
</code></pre>

<p>EDIT2: To fix what was wrong Re. Matthieu, I have changed my naive implementation like so:</p>

<pre><code>size_t mbsnrtowcs( wchar_t *__restrict__ dst, const char **__restrict__ src,
                   size_t nmc, size_t len, mbstate_t *__restrict__ ps )
{
    char* local_src = new char[nmc+1];
    char* nmcsrc = local_src;
    strncpy( nmcsrc, *src, nmc );
    nmcsrc[nmc] = '\0';
    const size_t result = mbsrtowcs( dst, const_cast&lt;const char **&gt;(&amp;nmcsrc), len, ps );
    // propagate error
    if( nmcsrc == NULL )
        *src = NULL;
    delete[] local_src;
    return result;
}
</code></pre>

<p>And I have added a FIXME saying that the proper way to do this would be implementing it via mbrtowc.</p>
