I have a hard time figuring out the idea behind the
narrow() and
widen() functions. In particular, which encoding is assumed for the argument to
widen(). I'll try to ask this way:
Let
GLYPH stand for some specific glyph from the basic source character set, e.g. 'A'.
Let
LITERAL stand for the character encoding used by the compiler to encode narrow character literals (e.g. US-ASCII).
Assume a specific but arbitrary locale.
Let
MULTIBYTE be the multibyte character encoding that the locale assigns to narrow characters (e.g. UTF-8).
Let
WIDE be the character encoding that the loclae assigns to wide characters (e.g. UCS-4).
Which of the following two descriptions of
ctype<wchar_t>::widen(char) is then most correct?
1) Let
c be the encoding of
GLYPH with respect to the
LITERAL encoding. The result of
ctype<wchar_t>::widen(c) is then the encoding of GLYPH with respect to the
WIDE encoding.
2) Let
c be the encoding of
GLYPH with respect to the
MULTIBYTE encoding. Since
GLYPH is in the basic source character set, it can always be represented as a single byte. The result of
ctype<wchar_t>::widen(c) is then the encoding of
GLYPH with respect to the
WIDE encoding.
I understand that in most cases the distinction between these two meanings is unimportant, but I'd really like to know which of the two comes closest to the originally intended function of widen().
What I'd really like to know, is this:
The task is to write out an integer on a stream, surrounded by parentheses, and I want to do this in a completely general and portable way.
Is that even possible?
Is this the way to do it:
1 2 3 4
|
void parenthesize(std::wostream &out, int i)
{
out << out.widen('(') << i << out.widen(')');
}
| |