Выбрать главу

You only need to be concerned about char_traits if you access the low-level character processing functions; most of the time you don’t care. Consider, however, making your inserters and extractors more robust by defining them as templates, just in case someone wants to use them on a wide stream.

To illustrate, recall again the Date class inserter from the beginning of this chapter. We originally declared it as:

ostream& operator<<(ostream&, const Date&);

This accommodates only narrow streams. To make it generic, we simply make it a template based on basic_ostream:

template<class charT, class traits>

std::basic_ostream<charT, traits>&

operator<<(std::basic_ostream<charT, traits>& os,

           const Date& d) {

  charT fillc = os.fill(os.widen('0'));

  charT dash = os.widen('-');

  os << setw(2) << d.month << dash

     << setw(2) << d.day << dash

     << setw(4) << d.year;

  os.fill(fillc);

  return os;

}

Notice that we also have to replace char with the template parameter charT in the declaration of fillc, since it could be either char or wchar_t, depending on the template instantiation being used.

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<class charT, class traits>

basic_ostream<charT,traits>&

nl(basic_ostream<charT,traits>& os) {

  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 locale. All streams have an associated locale object that they use for guidance on how to display certain quantities for different cultural environments. A locale manages the categories of culture-dependent display rules, which are defined as follows:

Category Effect
collate allows comparing strings according to different, supported collating sequences
ctype abstracts the character classification and conversion facilities found in <cctype>
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 <iostream>

#include <locale>

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 facets, which are classes encapsulating the functionality that pertains to that category. For example, the time category has the facets time_put and time_get, which contain functions for doing time and date input and output respectively. The monetary category has facets money_get, money_put, and moneypunct. (The latter facet determines the currency symbol.) The following program illustrates the moneypunct facet. (The time facet requires a sophisticated use of iterators which is beyond the scope of this chapter.)