Here is a piece of code that uses a facet std::codecvt_utf8<>to convert from wchar_tto UTF-8. In Visual Studio 2012, my expectations are not fulfilled (see the Condition at the end of the code). Are my expectations wrong? What for? Or is it a Visual Studio 2012 library issue?
#include <locale>
#include <codecvt>
#include <cstdlib>
int main ()
{
std::mbstate_t state = std::mbstate_t ();
std::locale loc (std::locale (), new std::codecvt_utf8<wchar_t>);
typedef std::codecvt<wchar_t, char, std::mbstate_t> codecvt_type;
codecvt_type const & cvt = std::use_facet<codecvt_type> (loc);
wchar_t ch = L'\u5FC3';
wchar_t const * from_first = &ch;
wchar_t const * from_mid = &ch;
wchar_t const * from_end = from_first + 1;
char out_buf[1];
char * out_first = out_buf;
char * out_mid = out_buf;
char * out_end = out_buf + 1;
std::codecvt_base::result cvt_res
= cvt.out (state, from_first, from_end, from_mid,
out_first, out_end, out_mid);
if (cvt_res == std::codecvt_base::partial
&& out_mid == out_end
&& state != 0)
;
else
abort ();
}
The function is expected to out()output one byte of UTF-8 conversion at a time, but in the middle of the conditional expression ifabove is false with Visual Studio 2012.
UPDATE
Unable to meet the conditions out_mid == out_endand state != 0. Basically, I expect at least one byte to be created, and the necessary state for the next byte of the UTF-8 sequence to be produced will be stored in a variable state.