Apache C++ Standard Library User's Guide

## 42.1 Locales and Iostreams

Within the C++ standard iostreams, locale is a class. Class locale is used for two distinct purposes:

- The formatting layer of iostreams uses locale facilities for numeric formatting and character classification.

- The transport layer uses locale facilities for code conversion.

In the standard iostream class templates, the formatting (stream) layer uses only the ctype, num_put, and num_get facets, while the transport (stream buffer) layer uses only the codecvt facet. For more information on locales and facets, see Chapter 24 and the sections of the Apache C++ Standard Library Reference Guide devoted to the locale class and the various facets.

Each layer contains its own locale, so the code conversion facility can be changed independent of numeric formatting. We simply change the locale associated with a stream buffer without altering the locale contained by the stream using that buffer. On the other hand, changing a stream's locale also changes the locale contained by the associated stream buffer.

The base class for all streams, ios_base, contains a locale object. This locale object can be accessed with the `getloc()` member function and replaced with the `imbue()` member function. Similarly, basic_streambuf also contains a locale object and it, too, defines `getloc()` and `imbue()` member functions (the latter is `protected` and `virtual` and called from the publicly accessible `pubimbue()` member function).