Since you don’t know when you’re writing the template which type of stream you have, you need a way to automatically convert character literals to the correct size for the stream. This is the job of the widen( ) member function. The expression widen('-'), for example, converts its argument to L’-’ (the literal syntax equivalent to the conversion wchar_t(‘-’)) if the stream is a wide stream and leaves it alone otherwise. There is also a narrow( ) function that converts to a char if needed.
We can use widen( ) to write a generic version of the nl manipulator we presented earlier in the chapter.
template
basic_ostream
nl(basic_ostream
return os << charT(os.widen('\n'));
}
Locales
Perhaps the most notable difference in typical numeric computer output from country to country is the punctuator used to separate the integer and fractional parts of a real number. In the United States, a period denotes a decimal point, but in much of the world, a comma is expected instead. It would be quite inconvenient to do all your own formatting for locale-dependent displays. Once again, creating an abstraction that handles these differences solves the problem.
That abstraction is the
Category | Effect |
collate | allows comparing strings according to different, supported collating sequences |
ctype | abstracts the character classification and conversion facilities found in |
monetary | supports different displays of monetary quantities |
numeric | supports different display formats of real numbers, including radix (decimal point) and grouping (thousands) separators |
time | supports various international formats for display of date and time |
messages | scaffolding to implement context-dependent message catalogs (such as for error messages in different languages) |
The following program illustrates basic locale behavior:
//: C04:Locale.cpp
//{-g++}
//{-bor}
//{-edg}
// Illustrates effects of locales
#include
#include
using namespace std;
int main() {
locale def;
cout << def.name() << endl;
locale current = cout.getloc();
cout << current.name() << endl;
float val = 1234.56;
cout << val << endl;
// Change to French/France
cout.imbue(locale("french"));
current = cout.getloc();
cout << current.name() << endl;
cout << val << endl;
cout << "Enter the literal 7890,12: ";
cin.imbue(cout.getloc());
cin >> val;
cout << val << endl;
cout.imbue(def);
cout << val << endl;
} ///:~
Here’s the output:
C
C
1234.56
French_France.1252
1234,56
Enter the literal 7890,12: 7890,12
7890,12
7890.12
The default locale is the "C" locale, which is what C and C++ programmers have been used to all these years (basically, English language and American culture). All streams are initially "imbued" with the "C" locale. The imbue( ) member function changes the locale that a stream uses. Notice that the full ISO name for the "French" locale is displayed (that is, French used in France vs. French used in another country). This example shows that this locale uses a comma for a radix point in numeric display. We have to change cin to the same locale if we want to do input according to the rules of this locale.
Each locale category is divided into number of
//: C04:Facets.cpp
//{-bor}
//{-g++}
#include
#include
#include
using namespace std;
int main() {
// Change to French/France
locale loc("french");
cout.imbue(loc);
string currency =
use_facet
char point =
use_facet
cout << "I made " << currency << 12.34 << " today!"
<< endl;
} ///:~
The output shows the French currency symbol and decimal separator:
I made Ç12,34 today!