codecvt mbstate_t values

J

JH Trauntvein

I recently learned about the codecvt locale facet by reading Jossutis'
(forgive me if I just misspelled his name) book on the C++ standard
library (this book has been worth its weight in gold by the way and I
would like to thank him for writing it). However, the book didn't have
much by the way of examples of using various methods of this facet. I
therefore turned to the documentation that comes with visual studio
..Net.

Several functions in that facet, in(), out(), length(), and etc.
require a reference parameter of mbstate_t. The documentation that I
have read says nothing about values for this variable and I assume that
the actual values used will vary by multi-byte encoding scheme. The
question that I have is what the default value of this variable should
be. Its purpose is to carry a state between calls so I assume that it
must have some sort of default value that identifies an appropriate
starting state. I have assumed that the value of zero will work but
this is a guess. Is my assumption about the default value correct?

Regards,

Jon Trauntvein
 
J

JH Trauntvein

JH said:
I recently learned about the codecvt locale facet by reading Jossutis'
(forgive me if I just misspelled his name) book on the C++ standard
library (this book has been worth its weight in gold by the way and I
would like to thank him for writing it). However, the book didn't have
much by the way of examples of using various methods of this facet. I
therefore turned to the documentation that comes with visual studio
.Net.

Several functions in that facet, in(), out(), length(), and etc.
require a reference parameter of mbstate_t. The documentation that I
have read says nothing about values for this variable and I assume that
the actual values used will vary by multi-byte encoding scheme. The
question that I have is what the default value of this variable should
be. Its purpose is to carry a state between calls so I assume that it
must have some sort of default value that identifies an appropriate
starting state. I have assumed that the value of zero will work but
this is a guess. Is my assumption about the default value correct?

I just tried to compile the same code under gcc 4.x and found that
compiler very unhappy when I initialised a value of type mbstate_t
using a scalar zero. Apparently, that implementation uses a structure
or an object to track the state where the M$ implementation uses some
sort of enumeration (again this is an assumption on my part).
 
P

P.J. Plauger

I just tried to compile the same code under gcc 4.x and found that
compiler very unhappy when I initialised a value of type mbstate_t
using a scalar zero. Apparently, that implementation uses a structure
or an object to track the state where the M$ implementation uses some
sort of enumeration (again this is an assumption on my part).

The C++ Standard is unfortunately silent on the required properties
of an mbstate_t object, so g¢¢ has every right to make it a struct
(which cannot be initialized with a scalar zero). If you look closer
at the Microsoft implementation (which we provide) you'll see that
we initialize a state object for a codecvt facet by declaring a
static copy of the state object with no initializer. In <fstream>
look for the line that reads:

static typename _Traits::state_type _Stinit; // initial state

We figure that's a pretty safe bet for most sensible flavors of
mbstate_t.

In fact, our CoreX library includes dozens of codecvt facets that
we've managed to make work with most Standard C++ libraries by
using this trick and never using more than the first byte of
the object.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top