Proposed Approached to Wide-Character Streams X3J16/93-0112 WG21/N0319 29 July 1993 P.J. Plauger There is strong sentiment for adding wide-character analogs to the current single-byte iostreams. I have written enough code that I think I now know how best to go about it. (It was spelled out in the draft I brought to Munich.) Briefly: >> Add wide-character input and output streams to streambuf That makes an enhanced class that controls up to four streams single-byte/wide-character input/output. If only one pair of streams at a time were usable, that would be rather inelegant. Instead, I propose that the streams be cross coupled as follows: -- Reading from a stream that isn't connected to a real source of characters causes a read attempt from the other stream. A wide character is converted to a single-byte by calling wctob (in the Normative Addendum). Single-byte is converted to wide character by calling btowc (ditto). If the read from the other stream succeeds, and if the conversion is permissible, the read succeeds. Putback works about the same, only in reverse. -- Writing to a stream that isn't connected to a real sink of characters causes a conversion and a write attempt to the other stream. Now, what happens in real life is that a streambuf is always specialized for a particular source and or sink of characters. Right now, only single-byte streams are supported. The few inserters and extractors we have that deal in (raw) wide characters attempt to read or write the wide streams instead. If the conversion can be made one-to-one, the operation succeeds. (This is much simpler, and more likely to meet pedestrian needs, than the current complex rules for performing wide I/O to the existing single-byte streams. They indulge in all sorts of fragile complexities regarding shift sequences.) A new specialization of streambuf, wfilebuf, can be connected to the new wide-character streams defined by the Normative Addendum to C. These deal directly in wide-characters (assembled and disassembled within the FILE objects as needed from/to multibyte files). Reading and writing wide characters is transparent, reading and writing raw single-byte characters is derived. The important simplification, however, comes with the inserters and extractors that deal with formatted data. These work just fine as single-byte functions. No need to add a whole new set of operators to support wide characters. Another new specialization of streambuf, wstrstreambuf, lets you manipulate wide-character sequences in memory with equal ease. >> Add wget* to istream and wput* to ostream We do want to add some new member functions to istream and ostream. It's hard to overload the existing get and put functions, given their signatures, to distinguish wide I/O from narrow. Thus, the interface is actually simpler if we introduce a new set of names for these relatively small groups of functions. >> Add wcin and wcout These two new objects are an istream and an ostream, just like cin and cout. The only difference is that they each have a wfilebuf connected to one of the standard streams. The net effect of this set of changes is to add new versions of only the smallest classes in iostreams. All the big classes (ios, streambuf, istream, ostream) get only modest additions, but need not be replicated. Moreover, this approach introduces wide I/O with all the familiar notational convenience of existing iostreams, at only modest cost in conceptual complexity. If this approach is found acceptable, I can provide a more complete specification on short notice.