0

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?

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
Basilevs
  • 22,440
  • 15
  • 57
  • 102
  • The C++ Standard is remarkably reticent when it comes to mbstate_t. The type is in fact defined by the C Standard. I have re-tagged your question accordingly. –  Aug 23 '09 at 19:14

1 Answers1

1

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
  • 22,897
  • 2
  • 80
  • 94
  • 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 Aug 24 '09 at 02:34