"Howard" <al*****@hotmail.com> wrote in message news:<c0********************@bgtnsc04-news.ops.worldnet.att.net>...
[ ... ]
I'm not sure what that sentence above means. What's a "facet"? And
what's a "locale"? And how do you "imbue" a stream with it? Sorry, but I
just don't recognize those terms (at least not in the context of C++
programming).
A locale describes how a set of characters in a character set should
be treated, interpreted, etc.
A locale consists of a number of facets -- each facet describes,
well...one facet of characters in that set. One facet describes the
basics of characters, such as which characters are considered letters,
digits, white-space, etc., in that character set. There are also
facets for how to read/write numbers, time values, how characters are
sorted, etc.
In your case, you want some custom processing done in reading numbers
from a stream. To do that, you start by creating a num_get facet,
which is object of a class derived (indirectly) from
std::locale::facet. It'll have to include some functions that take
input characters and convert them to numbers.
Using that is done in two steps. Each iostream has an associated
locale. Therefore, you have to 1) create a locale, and 2) tell the
istream to use that locale.
To create the locale, you usually specify a locale that uses the
facets from the default locale for everything except the thing(s) you
care about -- in this case, reading numbers. std::locale has a ctor
to do exactly that.
Having created a complete locale that uses your routines in its
num_get facet, you then have to tell the stream to use that locale --
this is done with a stream member function named imbue, so it's
generally called imbuing the stream with the locale.
This is a part of the library that many books completely or partly
skip over. There's a fair amount of "stuff" that you have to learn
before it all makes sense, and some of it perplexing even after you've
studied it (I'm pretty sure I still don't entirely understand codecvt
facets) but quite a bit of it is fairly easy to use once you learn
about it, and it really can make some tasks a lot easier than would
otherwise be the case. In particular, they allow you to modify
specific parts of how input or output is done in isolation. OTOH,
some (as Howard Hinnant hinted at) are difficult to get right -- he's
clearly a smart guy, and doesn't shrink away from doing some hard
programming when needed, so I (for one) wouldn't blithely ignore it
when he hints that someting is particularly difficult.
--
Later,
Jerry.
The universe is a figment of its own imagination.