Using iconv while maintaining code correctness
I'm c开发者_JS百科urrently using iconv
to convert documents with different encodings.
The iconv()
function has the following prototype:
size_t iconv (
iconv_t cd,
const char* * inbuf,
size_t * inbytesleft,
char* * outbuf,
size_t * outbytesleft
);
So far, I only had to convert buffers of type char*
but I also realized I could have to convert buffers of type wchar_t*
. In fact, iconv
even has a dedicated encoding name "wchar_t"
for such buffers: this encoding adapts to the operating system settings: that is, on my computers, it refers to UCS-2 on Windows and to UTF-32 on Linux.
But here lies the problem: if I have a buffer of wchar_t*
I can reinterpret_cast
it to a buffer of char*
to use it in iconv
, but then I face implementation defined behavior: I cannot be sure that the all compilers will behave the same regarding the cast.
What should I do here ?
reinterpret_cast<char const*>
is safe and not implementation defined, at least not on any real implementations.
The language explicitly allows any object to be reinterpreted as an array of characters and the way you get that array of characters is using reinterpret_cast
.
精彩评论