An example of when you would want to imbue a new locale on a stream might be the case where a file has a header that requires a different code conversion facet. The following code imbues the JIS to UNICODE conversion facet from Chapter 40 onto a stream, reads a single line, then imbues the default locale to continue processing the file:
std::wstring header; std::wstring body; typedef std::basic_ifstream<wchar_t,JIS_char_traits> JIS_ifstream; JIS_fstream in("special.txt"); UnicodeJISConversion<wchar_t,char,JISstate_t> cvtfac; std::locale cvtloc(std::locale(),&cvtfac); in.rdbuf()->pubimbue(cvtloc); std::getline(in,header); in.rdbuf()->pubimbue(std::locale()); in >> body;