tags:

views:

90

answers:

1

I'm writing an implementation of std::codecvt facet that uses iconv. This implementation stores a pointer to heap-allocated data in std::mbstate_t state argument.

Everything works fine, but is this code 64-bit compatible? Is there a platform where a pointer size exceeds the size of std::mbstate_t?

+1  A: 

Doesn't the codecvt template take the state type as a parameter? Can you just use a pointer type there instead? I can't remember whether the various classes that use a codecvt place requirements on the state type.

Assuming that you can't just change the state type... on MSVC 2008, mbstate_t is typedefd as an int. The standard only requires that int be larger than 16 bits and no larger than a long, so it's not 64-bit safe. I guess you would need to store an index or key into some data structure instead of a pointer.

update:

The following compiles under VS2008, at least:

std::wstring const in = L"input";
size_t const buf_size = 256;
char* buf = new char[buf_size];
wchar_t const* char_next;
char * byte_next;
void* state = NULL;

typedef std::codecvt<wchar_t, char, void*> codecvt_t;
codecvt_t::result res =
    std::use_facet<codecvt_t>(std::locale()).out(
     state, in.c_str(), in.c_str() + in.length(),
     char_next, &buf[0], &buf[buf_size], byte_next);
Tim Sylvester
Codecvt with state type different from std::mbstate_t is essentially useless in terms of standart streambuf implemetations. Using another state type will force you to reimplement streambuf ancestors.
Basilevs