Proposed Approach for Locale Control in Streams X3J16/93-0111 WG21/N0318 29 July 1993 P.J. Plauger There is strong sentiment for associating tailored locales with at least certain I/O streams. The LWG has agreed to explore adding a kind of stream class that has locale-dependent behavior. It too is a candidate for having its own tailored locale. (It is also a candidate for connecting to a streambuf, of course.) The idea is to have member functions that manipulate these objects switch part or all of the current locale to the tailored locale while performing locale-dependent operations on them, then switch back when the operation is complete. I have written enough code using such machinery 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 the bitmask type ios::localecat The C Standard is vague about the nature of the category argument to setlocale, but X/OPEN has made an important clarification. They insist that a category is a bitmask, so that one scalar quantity can represent an arbitrary combination of categories. I think the C++ Standard should endorse that position and add the bitmask type localecat to ios, with elements that name all the individual categories. The constant all would have the value ~0. >> Add ios::localecat ios::locale(const char *s, ios::localecat cat) This function memorizes the locale s, for categories specified by cat, as the tailored locale for the controlled stream. The actual work should be done by a virtual member of streambuf, because locales should be tailored that way. The (derived) streambuf may control a C file with its own locale, or a locale-dependent string object. What you control and how you control it is thus dependent on the actual streambuf. Still, the access function more naturally lives in ios, as the LWG group concluded some time ago. All this function returns is the current category profile. (If s is null, the call is purely a query.) Thus, the underlying machinery is at liberty to store a more encoded representation of the locale, not the arbitrary length string s which may require considerable interpretation when used to specify a locale. That lets implementations optimize heavily if they see extensive use of locales, while simpler implmenentations can simply store and use the string, at whatever cost in performance. The default for each streambuf should be no locale (category is zero). Hence, the stream follows the current convention of using the current locale for all operations. >> Add ios::setlocale(int changefl = 0) The call ios::setlocale(1), typically within ipfx() or opfx(), memorizes the current locale and switches the current locale to match the memorized profile. The call ios::setlocale(0) reverts to the memorized locale. (Such a call also occurs in setstate/clear, before an exception is thrown or rethrown.) This is the mechanism used by all the member functions to switch the desired locale in and out. It too actually calls a virtual in streambuf, for the same reasons as cited above. If this approach is found acceptable, I can provide a more complete specification on short notice.